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.
| Algorithm | Best | Average | Worst | Space | Stable | In place |
|---|---|---|---|---|---|---|
| Bubble sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Selection sort | O(n²) | O(n²) | O(n²) | O(1) | No | Yes |
| Insertion sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes |
| Merge sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No |
| Quick sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | Yes |
| Heap sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | Yes |
| Counting sort | O(n + k) | O(n + k) | O(n + k) | O(n + k) | Yes | No |
| Radix sort | O(d (n + k)) | O(d (n + k)) | O(d (n + k)) | O(n + k) | Yes | No |
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.
| Algorithm | Best | Average | Worst | Space | Needs sorted input |
|---|---|---|---|---|---|
| Linear search | O(1) | O(n) | O(n) | O(1) | No |
| Binary search | O(1) | O(log n) | O(log n) | O(1) | Yes |
| Breadth-first search | O(V + E) | O(V + E) | O(V + E) | O(V) | — |
| Depth-first search | O(V + E) | O(V + E) | O(V + E) | O(V) | — |
| Dijkstra's algorithm | O(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.
| Structure | Access | Search | Insert | Delete | Space |
|---|---|---|---|---|---|
| Stack | O(1)the top element only, with peek | O(n)only the top is exposed, so a value underneath is reached by popping everything above it | O(1)push writes one slot and the length | O(1)pop reads one slot and the length | O(n) |
| Queue | O(1)the front element only, with peek | O(n)only the front is exposed, so a value behind it is reached by removing the ones before it | O(1)enqueue writes one slot at the back | O(1)dequeue moves the head forward by one | O(n) |
| Binary search tree | O(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 nodes | O(log n) / O(n)walks the same path as search, then links one new leaf | O(log n) / O(n)finds the node on one path, then relinks; the worst case is again a degenerate tree | O(n) |
| Binary heap | O(1)the minimum, at the root of the array | O(n)a heap is not sorted, so any value other than the minimum takes a scan of the array | O(log n)sift-up walks at most one path of about log n levels | O(log n)extracting the minimum moves the last value to the root, which then sifts down one path | O(n) |
| Linked list | O(n)the k-th node is reached by following k links from the head; a list gives no way to jump ahead | O(n)walks from the head; the best case is a target in the first node | O(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 table | not applicableno k-th element: the keys have no order | O(1) / O(n)one hash, then a chain of about n / m nodes; the worst case is every key in one bucket | O(1)the new key goes at the front of its bucket's chain, with no loop | O(1) / O(n)walks one chain to find the key, so it costs what search costs | O(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.
O(1) Constant — the work does not depend on n at all
O(log n) Logarithmic — each step throws away a fixed fraction of what is left
O(n) Linear — the work is proportional to the number of elements
O(n + k) Linear in n and the value range — one pass over the n values and one over the k possible values, with no comparisons
O(d (n + k)) Linear per digit — d passes of a counting sort, one for each digit position
O(V + E) Linear in the graph — every node and every edge is looked at a constant number of times
O(n log n) Linearithmic — about log n rounds, each doing linear work
O(V²) Quadratic in the nodes — for each of the V nodes, a scan of all V nodes
O(n²) Quadratic — roughly every element is compared with every other
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.
| Growth | n = 1,000 | n = 1,000,000 |
|---|---|---|
| O(log n) | under a millisecond | under a millisecond |
| O(n) | under a millisecond | 1 milliseconds |
| O(n log n) | under a millisecond | 20 milliseconds |
| O(n²) | 1 milliseconds | 17 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
| Growth | Operations for n = 16 |
|---|---|
| O(1) Constant | 1 |
| O(log n) Logarithmic | 4 |
| O(n) Linear | 16 |
| O(n log n) Linearithmic | 64 |
| O(n²) Quadratic | 256 |
| O(2ⁿ) Exponential | 65,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.