← All animations

📈 Big-O Notation

Big-O describes how an algorithm's work grows as its input n gets large. Two programs can solve the same problem, yet one finishes before lunch while the other outlives the universe — purely because of how their cost scales. Slide n below and watch the gulf between O(1) and O(2ⁿ) open up: the same input that costs one class a handful of steps costs another more steps than there are atoms in the galaxy.

1. Racing growth curves — the same n, wildly different cost

Six complexity classes plotted on one chart. Drag n to sweep the vertical line; the cards below read out the exact number of steps each class needs at that n. The fast curves hug the floor while O(n²) and O(2ⁿ) rocket off the top — flip log scale on to keep the runaways on screen and see them as straight lines.

n = 32

2. What they look like in real code — and how long they take

Every class maps to operations you already use. The right column shows the wall-clock time at a brisk 1 billion steps/second for the n you pick below — turning abstract step counts into seconds, years, and beyond.

ClassFamiliar exampleIdea
O(1)Array index arr[i], hash-map lookupCost is constant — independent of n.
O(log n)Binary search in a sorted arrayHalve the search space each step.
O(n)Linear scan / sum a listTouch every element once.
O(n log n)Merge sort, quicksort (avg)The best general comparison sorts.
O(n²)Nested loops, bubble sortCompare every pair.
O(2ⁿ)Enumerate every subsetEach item doubles the work.
O(n!)Brute-force travelling salesmanTry every ordering of n items.

Time to run, at 10⁹ steps/sec, for the chosen n:

ClassSteps at nTime @ 1e9/s
n = 50

3. The idea in three sentences

Big-O is about the shape of growth, not exact stopwatch numbers.

Drop constants and lower-order terms. A loop doing 3n + 7 steps and one doing n steps are both O(n): as n→∞ the leading term dominates and the 3× and the +7 stop mattering. n² + n + 500 is just O(n²).
Worst vs average case. Quicksort is O(n log n) on average but O(n²) in its worst case; a hash lookup is O(1) average, O(n) if everything collides. Big-O usually quotes the worst case — the guarantee you can rely on.
It matters at scale, not for tiny inputs. For n = 5, an O(2ⁿ) algorithm beats a fancy O(n log n) one — constants win when n is small. The classes only diverge as n grows, which is exactly when you care: a 2× faster computer rescues an O(n) program but barely dents an O(2ⁿ) one.