Big O Cheat Sheet

Interactive time and space complexity reference for algorithms and data structures.

Complexity Growth Chart

050100150200250300051015202530Elements (n)Operations
O(1)O(log n)O(n)O(n log n)O(n²)O(2ⁿ)

Sorting Algorithms

AlgorithmBestAverageWorstSpaceStable
Bubble SortO(n)O(n²)O(n²)O(1)Yes
Selection SortO(n²)O(n²)O(n²)O(1)No
Insertion SortO(n)O(n²)O(n²)O(1)Yes
Merge SortO(n log n)O(n log n)O(n log n)O(n)Yes
Quick SortO(n log n)O(n log n)O(n²)O(log n)No
Heap SortO(n log n)O(n log n)O(n log n)O(1)No
Counting SortO(n+k)O(n+k)O(n+k)O(k)Yes
Radix SortO(nk)O(nk)O(nk)O(n+k)Yes
Tim SortO(n)O(n log n)O(n log n)O(n)Yes

Data Structure Operations

Each cell shows Average / Worst case complexity.

Data StructureAccessSearchInsertDelete
ArrayO(1)/O(1)O(n)/O(n)O(n)/O(n)O(n)/O(n)
StackO(n)/O(n)O(n)/O(n)O(1)/O(1)O(1)/O(1)
QueueO(n)/O(n)O(n)/O(n)O(1)/O(1)O(1)/O(1)
Linked ListO(n)/O(n)O(n)/O(n)O(1)/O(1)O(1)/O(1)
Hash Table/O(1)/O(n)O(1)/O(n)O(1)/O(n)
BSTO(log n)/O(n)O(log n)/O(n)O(log n)/O(n)O(log n)/O(n)
AVL TreeO(log n)/O(log n)O(log n)/O(log n)O(log n)/O(log n)O(log n)/O(log n)
B-TreeO(log n)/O(log n)O(log n)/O(log n)O(log n)/O(log n)O(log n)/O(log n)
Red-BlackO(log n)/O(log n)O(log n)/O(log n)O(log n)/O(log n)O(log n)/O(log n)

Common Algorithms

AlgorithmTimeSpace
Binary SearchO(log n)O(1)
BFSO(V+E)O(V)
DFSO(V+E)O(V)
DijkstraO((V+E)log V)O(V)
Bellman-FordO(VE)O(V)
Floyd-WarshallO(V³)O(V²)

Interactive Chart

Hover over the growth chart to compare operations at any input size.

Common Complexities

O(1) to O(n!) — all major complexity classes with color-coded badges.

Code Examples

Sorting algorithms, data structures, and graph algorithms at a glance.

Comparison

Side-by-side tables for best, average, worst case and space complexity.

About Big O Cheat Sheet

The Big O Notation Cheat Sheet is a comprehensive reference for algorithm and data structure time and space complexity. Big O notation describes how an algorithm's performance scales with input size — understanding it is essential for writing efficient code, passing technical interviews, and making informed architectural decisions. This tool provides an interactive growth chart where you can hover to compare how many operations different complexity classes require at any input size, plus reference tables for 9 sorting algorithms, 9 data structures, and 6 common graph and search algorithms. Each entry shows best, average, and worst-case time complexity and space complexity with colour-coded badges from green (efficient O(1), O(log n)) to red (avoid O(n²), O(2ⁿ)). Stability of sorting algorithms is also noted.

How to Use Big O Cheat Sheet

  1. Hover over the Complexity Growth Chart to see the exact operation count for each complexity class at a given input size n.
  2. Study the colour coding: green = fast (O(1), O(log n)), yellow = acceptable (O(n)), orange = moderate (O(n log n)), red = slow (O(n²), O(2ⁿ)).
  3. Use the Sorting Algorithms table to compare sorting choices — remember Quick Sort has O(n²) worst case while Merge Sort guarantees O(n log n).
  4. Use the Data Structures table to choose the right structure — Hash Tables give O(1) average search but O(n) worst case; AVL Trees guarantee O(log n) for all operations.
  5. Check the Common Algorithms section for standard graph algorithm complexities (BFS, DFS, Dijkstra, Floyd-Warshall).
  6. Use the Stable column when selecting a sort for data where the order of equal elements matters.

Frequently Asked Questions

What is Big O notation and why does it matter?

Big O notation describes the upper bound of how an algorithm's runtime or memory usage grows as the input size n increases. It matters because an algorithm that works fine for 100 items may be completely unusable for 1,000,000 items. O(n²) at n=1000 means 1,000,000 operations; O(n log n) at n=1,000,000 means only ~20,000,000 — a 50× difference that becomes billions× at larger scales.

What is the difference between time complexity and space complexity?

Time complexity measures how the number of operations grows with input size. Space complexity measures how much extra memory the algorithm needs. For example, Merge Sort has O(n log n) time but O(n) space because it needs a temporary copy of the array. In-place algorithms like Heap Sort use O(1) extra space at the cost of more complex implementation.

Why does Hash Table have O(n) worst-case search?

Hash Tables achieve O(1) average search because keys are distributed across buckets using a hash function. In the worst case (a very poor hash function or many hash collisions), all keys end up in the same bucket, degenerating into a linked list with O(n) search. Good hash functions and load factor management (rehashing) keep the average near O(1).

When should I choose Merge Sort over Quick Sort?

Choose Merge Sort when: (1) you need guaranteed O(n log n) worst-case performance and cannot afford the O(n²) worst case of Quick Sort on sorted or adversarially chosen input, (2) you need a stable sort, or (3) you are sorting linked lists where random access is expensive. Choose Quick Sort when average performance matters more and you want in-place sorting with low cache miss rates.

What is amortized complexity and how does it relate to Big O?

Amortized complexity describes the average cost per operation over a sequence of operations, even if individual operations occasionally take longer. For example, dynamic arrays (like JavaScript arrays or Python lists) have O(1) amortized push even though they occasionally need O(n) to resize and copy — because resizes happen infrequently enough that the average cost per push is still O(1).

Related Tools

Algorithm VisualizerCode Snippet ManagerJSON Formatter