Big O, by counting
Big O describes how the number of basic operations grows with the input size, ignoring constant factors. The explorer draws the common classes; the exercises ask you to derive the class of a code snippet, then show the derivation. Every answer here was checked by running the snippet's loop structure and counting.
Growth curves
| Class | Name | f(n) |
|---|---|---|
| O(1) | constant | 1 |
| O(log n) | logarithmic | 5 |
| O(√n) | square root | 6 |
| O(n) | linear | 32 |
| O(n log n) | linearithmic | 160 |
| O(n²) | quadratic | 1,024 |
| O(n³) | cubic | 32,768 |
| O(2ⁿ) | exponential | 4,294,967,296 |
| O(n!) | factorial | 2.63e+35 |
Constant factors decide which algorithm wins for small n; growth decides it for large n. At n = 64, 2ⁿ is already about 1.8 × 10¹⁹: no constant factor rescues an exponential algorithm.
Measure an exercise
Each worked exercise below has a counter that runs its loop structure. Here its measured counts are drawn against its claimed class: if the claim is right, the two curves have the same shape.
The interactive explorer needs JavaScript; the exercises below work without it.
O, Θ and Ω in one paragraph each
O(g(n)) is an upper bound: for large enough n, the count is at most a constant times g(n). Linear search is O(n), and also (uselessly) O(n²).
Ω(g(n)) is a lower bound: at least a constant times g(n). Any comparison sort is Ω(n log n) in the worst case.
Θ(g(n)) means both: the count grows exactly like g(n) up to constants. When people say "binary search is O(log n)" they usually mean Θ(log n) in the worst case.
A bound also needs a case (best, worst, average over stated inputs, expected over random choices, or amortized over a sequence) and a resource (time or extra space). The exercises name both.
Worked exercises (30)
Beginner
- One loop over the inputloops · linear
- Two loops one after the otherloops · linear · constants
- A loop inside a looploops · quadratic · nested
- Inner loop that depends on the outer indexloops · quadratic · arithmetic series
- Halving until one is leftloops · logarithmic
- Doubling a counterloops · logarithmic
- Stepping by threeloops · linear · constants
- Three nested loopsloops · cubic · matrix multiplication
- Sequential loops over two inputsmultiple inputs · linear
- Recursion that peels off one elementrecursion · call stack · linear
- Linear search that stops earlybest case · worst case · average case
- Filling an n × n tablespace · quadratic · dynamic programming
Intermediate
- A linear loop around a doubling looploops · linearithmic · nested
- Inner loop bounded by a halving outer variableloops · geometric series · linear
- Loop until i² exceeds nloops · square root · primes
- Nested loops over two different inputsmultiple inputs · nested
- Naive recursive Fibonaccirecursion · exponential · recursion tree
- Recursion on half the inputrecursion · logarithmic · recurrences
- Two halves plus linear workrecursion · linearithmic · merge sort
- Two halves plus constant workrecursion · linear · recurrences
- Generating every subsetrecursion · exponential · backtracking
- Generating every permutationrecursion · factorial · backtracking
- Concatenating immutable strings in a loopstrings · quadratic · hidden cost
- Inserting at the front of an array list, n timesdynamic arrays · quadratic · hidden cost
- n appends to a doubling dynamic arrayamortized · dynamic arrays · linear
- Sort once, then binary search n timessorting · binary search · linearithmic
- Nested loop with a breakloops · quadratic · break
- Two pointers moving toward each othertwo pointers · linear · potential argument