1. Counting operations
A stopwatch measures the machine, the language, the compiler, and the input. Big-O throws all of that away and keeps the part that survives: how the number of operations grows as the input size grows. If an algorithm does 3n² + 5n + 7 operations, the quadratic term dominates once n is large. The constants and the linear term are real, but they are not the shape.
The notation is deliberately coarse. It is not a promise about a particular input; it is a statement about a family of inputs. That is why the same algorithm can be O(n²) worst case and O(n) best case, as insertion sort is.
2. The definition
Big-O is an upper bound up to a constant factor. Formally, f(n) = O(g(n)) if there are positive constants c and n₀ such that f(n) is at most c·g(n) for every n ≥ n₀.
The related notations are worth keeping straight. Big-Omega is a lower bound, Big-Theta is a tight bound, and little-o is a strict upper bound. In practice, “this algorithm is O(n log n)” usually means “its worst-case running time grows no faster than n log n,” and often the speaker also believes it is Θ(n log n).
3. The ladder
These are the orders you will meet most often. The table is not a ranking of importance; it is a ranking of growth. A logarithmic algorithm can still be slower than a linear one on small inputs, because constants matter there. The order matters when n gets large.
- O(1) — array index, hash lookup (expected), stack push/pop.
- O(log n) — binary search, balanced-tree search.
- O(n) — linear scan, one pass over an array.
- O(n log n) — merge sort, heapsort, the comparison-sort lower bound.
- O(n²) — bubble/insertion sort, a double loop over the input.
- O(2ⁿ) — exhaustive subsets, naive recursive Fibonacci.
4. What Big-O hides
Big-O is a tool, not a complete performance model. It hides constants, lower-order terms, cache behavior, memory allocation, and the actual distribution of inputs. A O(n) algorithm with a huge constant can lose to a O(n log n) algorithm on the sizes you actually run.
- Constants — two O(n) loops can differ by a factor of 50.
- Lower-order terms — 3n² + 1000n is still O(n²), but the linear term may dominate for small n.
- Worst vs average — quicksort is O(n log n) average and O(n²) worst.
- Space — merge sort is O(n) extra memory; heapsort is O(1) extra.
- Amortized cost — a dynamic array append is O(1) amortized, even though one append occasionally copies the whole array.
Example: 3n² + 5n + 7 = O(n²)
We need c and n₀ such that 3n² + 5n + 7 ≤ c·n² for all n ≥ n₀. Try c = 4:
The positive root is (5 + √53)/2 ≈ 6.14, so n₀ = 7 works. The constants are not unique; c = 5 with n₀ = 3 also works. Any pair that eventually holds is enough.
5. Practice
What is the complexity of a loop that visits every element of an n-element array once?
What is the complexity of two nested loops, each running n times?
Binary search halves the remaining range each step. What is its complexity?
A hash table has n keys in m buckets with chaining. What is the expected lookup cost, and when does it degrade?
Merge sort satisfies T(n) = 2T(n/2) + O(n). What is T(n)?
6. Resources
The next lesson makes the cost model concrete with the structures you use every day. Keep the ladder in mind: the data structure chooses which operations are cheap, and the algorithm chooses how many times you pay for them.