Skip to content
SimpleScope

Big-O cheat sheet

The time and space complexity of every algorithm and data structure on this site, in four tables: sorting, searching and graphs, data-structure operations and what each growth class means. Every figure is the one its own lesson states, because both are read from the same file.

Big-O describes how the work grows as the input grows. It drops constants and small terms and keeps the part that dominates for large inputs, so it tells you which algorithm keeps up when the data gets big — not which is faster on ten elements.

Sorting algorithm complexity

8 sorting algorithms. Stable means equal values keep their original order. In place means it sorts inside the input array, with only a constant amount of extra room.

Sorting algorithm complexity
AlgorithmBestAverageWorstSpaceStableIn place
Bubble sortO(n)O(n²)O(n²)O(1)YesYes
Selection sortO(n²)O(n²)O(n²)O(1)NoYes
Insertion sortO(n)O(n²)O(n²)O(1)YesYes
Merge sortO(n log n)O(n log n)O(n log n)O(n)YesNo
Quick sortO(n log n)O(n log n)O(n²)O(log n)NoYes
Heap sortO(n log n)O(n log n)O(n log n)O(1)NoYes
Counting sortO(n + k)O(n + k)O(n + k)O(n + k)YesNo
Radix sortO(d (n + k))O(d (n + k))O(d (n + k))O(n + k)YesNo

No comparison sort can beat O(n log n) in the worst case. Counting and radix sort get under it by never comparing two values, which is why their figures are in terms of k, the number of possible values (or digit values), and d, the number of digit positions. They buy that speed with assumptions about the keys and with extra memory.

Searching and graph algorithm complexity

Stability and in-place do not apply to these, so they get their own table rather than empty columns. V is the number of nodes and E the number of edges.

Searching and graph algorithm complexity
AlgorithmBestAverageWorstSpaceNeeds sorted input
Linear searchO(1)O(n)O(n)O(1)No
Binary searchO(1)O(log n)O(log n)O(1)Yes
Breadth-first searchO(V + E)O(V + E)O(V + E)O(V)—
Depth-first searchO(V + E)O(V + E)O(V + E)O(V)—
Dijkstra's algorithmO(V²)O(V²)O(V²)O(V)—

Dijkstra is shown with the simple list used in its lesson, which scans every node each round. Replacing that list with a binary heap makes it O((V + E) log V), faster on sparse graphs.

Data structure operations

Average and worst case per operation, where they differ. A single figure means the two are the same. The note under each one says what the operation actually does on that structure, because “search” on a stack is not the same job as search on a hash table.

Data structure operations
StructureAccessSearchInsertDeleteSpace
StackO(1)the top element only, with peekO(n)only the top is exposed, so a value underneath is reached by popping everything above itO(1)push writes one slot and the lengthO(1)pop reads one slot and the lengthO(n)
QueueO(1)the front element only, with peekO(n)only the front is exposed, so a value behind it is reached by removing the ones before itO(1)enqueue writes one slot at the backO(1)dequeue moves the head forward by oneO(n)
Binary search treeO(log n) / O(n)by key, down one path; a tree has no index, so reaching the k-th key in order means an in-order walk, O(n)O(log n) / O(n)a balanced tree has a height of about log n; a degenerate tree is a linked list of n nodesO(log n) / O(n)walks the same path as search, then links one new leafO(log n) / O(n)finds the node on one path, then relinks; the worst case is again a degenerate treeO(n)
Binary heapO(1)the minimum, at the root of the arrayO(n)a heap is not sorted, so any value other than the minimum takes a scan of the arrayO(log n)sift-up walks at most one path of about log n levelsO(log n)extracting the minimum moves the last value to the root, which then sifts down one pathO(n)
Linked listO(n)the k-th node is reached by following k links from the head; a list gives no way to jump aheadO(n)walks from the head; the best case is a target in the first nodeO(1)at the head: one new node and two links. Inserting at the tail walks the list, O(n)O(n)removing a value means walking to the node before it; removing the head alone is O(1)O(n)
Hash tablenot applicableno k-th element: the keys have no orderO(1) / O(n)one hash, then a chain of about n / m nodes; the worst case is every key in one bucketO(1)the new key goes at the front of its bucket's chain, with no loopO(1) / O(n)walks one chain to find the key, so it costs what search costsO(n)

Space is what the structure itself occupies for n elements, not extra room per operation. The binary search tree here is unbalanced, so its worst case is a tree that has degenerated into a list; a self-balancing tree such as a red-black tree holds O(log n) for all three. The hash table uses chaining, and its worst case is every key landing in one bucket.

What each growth class means

Grouped by average-case time, slowest-growing first. Each algorithm appears under its own average case, taken from the same table above.

What the classes cost in time

Assuming a computer that does a billion simple operations per second. Real programs are slower, but the proportions are the same.

Time taken by each growth class at two input sizes
Growthn = 1,000n = 1,000,000
O(log n)under a millisecondunder a millisecond
O(n)under a millisecond1 milliseconds
O(n log n)under a millisecond20 milliseconds
O(n²)1 milliseconds17 minutes

See the growth

Move the slider. The curves that leave the chart are the ones that stop being practical.

  • O(1)Constant
  • O(log n)Logarithmic
  • O(n)Linear
  • O(n log n)Linearithmic
  • O(n²)Quadratic
  • O(2ⁿ)Exponential
Operations needed for n = 16
GrowthOperations for n = 16
O(1) Constant1
O(log n) Logarithmic4
O(n) Linear16
O(n log n) Linearithmic64
O(n²) Quadratic256
O(2ⁿ) Exponential65,536

How to read a complexity

  • Drop constants. Doing twice the work is still linear: O(2n) is O(n).
  • Keep the dominant term. n² + n is O(n²), because n² wins for large n.
  • Nested loops multiply. A loop over n inside a loop over n is O(n²).
  • Halving means a logarithm. Cutting the problem in half each step takes about log n steps.
  • Say which case you mean. Best, average and worst can differ a lot: quick sort is O(n log n) on average and O(n²) in the worst case.
  • Space is extra space. The space column counts the room an algorithm needs beyond the input, which is why an in-place sort can be O(1) while merge sort is O(n).
  • A smaller class can still lose. Insertion sort beats merge sort on small or nearly sorted arrays, because Big-O hides the constants that dominate there.