Big-O notation summarizes how an algorithm scales as its input grows. Knowing the complexity of the operation you are about to run is the fastest way to predict whether code will stay fast at scale. This reference collects the standard complexities for the algorithms and data-structure operations you meet daily.
How it works
For each entry you get up to four figures:
- Best — the most favorable input (often nearly-sorted or an immediate hit).
- Average — expected cost over typical input.
- Worst — the pathological case you must design around.
- Space — additional memory beyond the input itself.
Lower-order terms and constants drop out: O(n + log n) is reported as O(n),
and O(2n) as O(n). The goal is the growth rate, not an exact count.
Example
Choosing a sort: merge sort guarantees O(n log n) time but needs O(n)
auxiliary memory and is stable. Heapsort also guarantees O(n log n) but sorts
in place with O(1) extra space, at the cost of stability. Quicksort is usually
fastest in practice but risks O(n²) without good pivoting. The table lets you
weigh these side by side.
Notes
- Amortized analysis explains why a dynamic-array append is “O(1)” despite
occasional
O(n)resizes — the cost is spread across many cheap operations. - Graph complexities are written in terms of
V(vertices) andE(edges); traversal isO(V + E), Dijkstra with a binary heap isO((V + E) log V). - Non-comparison sorts (counting, radix) beat the
O(n log n)comparison lower bound only because they exploit bounded key ranges. - Always check whether your real inputs hit the worst case before optimizing — the average case is what usually runs.