Imagine two friends each looking up the name "Zara" in a thick paper phone book. Alice starts at page one and reads every single name in order until she reaches Zara near the very end. Ben opens the book in the middle, sees he's landed on "M", and throws away the whole first half in one go — then repeats, halving what's left each time. Both find Zara. Both are correct. But Ben finishes in a handful of flips while Alice is still turning pages.
This is the heart of algorithm efficiency: two algorithms can solve the very same problem and both give the right answer, yet one does dramatically less work than the other. Choosing the clever one isn't showing off — for a big enough phone book it's the difference between an answer now and an answer next week. This page is about how we measure "less work" in a fair way, and how to name the patterns we find.
The obvious idea is to time each algorithm and pick the faster one. But a stopwatch measures the computer, not the algorithm. Run Alice's method on a brand-new gaming PC and Ben's on a ten-year-old laptop and you might "prove" that reading every page is quicker — which tells you nothing useful. The same code is faster on a faster machine, slower when the computer is busy, and different again in another programming language.
So computer scientists ignore seconds and count steps instead — the number of basic operations the algorithm performs, such as comparisons or swaps. And we don't care about the count for one particular input; we ask a sharper question:
As the input size
Here
Alice's method is linear search: check items one by one from the start. In the
worst case — the item is last, or missing entirely — she checks all
Ben's method is binary search: on a sorted list, jump to the middle and
throw away half. Each step throws away half of what remains, so the number of steps is how many
times you can halve
The catch (there's always a catch): binary search needs the list to be sorted first. Linear search works on any old jumble. Efficiency is often a trade — which is exactly why we compare.
Talk is cheap — let's count. The code below builds a sorted list of
Linear search grinds through hundreds of comparisons; binary search finishes in about ten. Try
changing n to 100000 and run again: the binary-search count barely
moves, while the linear count explodes. That gap is efficiency.
The same story plays out when we sort a list. Bubble sort repeatedly
walks the list swapping neighbours that are out of order. Because it makes roughly one pass per
item, and each pass looks at roughly every item, its work grows like
Merge sort is cleverer: it splits the list in half, sorts each half, then merges
them back together. All that splitting gives
We keep saying "about
Here are the ones you'll meet most often, slowest-growing (best) first:
Big-O describes a trend, not an exact count — an
The "O" is for order — as in "order of magnitude", the rough size of a thing. The notation dates back to a German number theorist, Paul Bachmann, in 1894, long before computers existed, and was popularised by Edmund Landau (so mathematicians sometimes call it a "Landau symbol"). Computer scientists borrowed it in the 1960s — Donald Knuth championed it — to talk about how algorithms scale. So this very modern-sounding idea is wearing a 130-year-old hat.
This is why growth rate is everything. The chart plots the number of steps against the input size
The take-away in one sentence: for small inputs the lines are close, but for large inputs
the growth rate wins by a mile. Pick the algorithm with the flatter curve and you win as
A "slower-looking" algorithm can genuinely be the right choice for small inputs.
Big-O only describes what happens as
A second habit worth building: we usually compare the worst case. Linear
search might get lucky and find the target on the very first try (its "best case" is