Growth of Functions and Big-O
Big-O cares about how a cost grows once the input gets large, not about constant factors or small cases. Saying f(n) = O(g(n)) means that, past some point, f never gets bigger than a constant times g. That's how you compare an algorithm taking n log n steps with one taking n² or 2ⁿ.
What the symbols mean
- Cost
Steps (or time, or memory) an algorithm uses on an input of size n.
- Growth rate
A simple comparison function such as n² or n log n.
- Constant factor
Any fixed multiplier; big-O ignores it.
- Threshold
Past this size the bound must hold; small inputs don't count.
Explanation
Big-O notation is simply a way to describe how fast an algorithm's work grows as you give it more to do. The "O" stands for "order," as in "order of growth": it sorts algorithms into rough classes like "grows like " or "grows like ." That's what makes it useful, because all we're really asking is: when the input gets big, how badly does the work pile up? The definition looks dense, but it's just a few pieces: (how many steps your program takes), (a simple yardstick like ), (a fudge factor you're allowed to pick) and (the point after which you start paying attention). "" just means that once inputs are big enough, stays under some multiple of . It's less scary than it looks; the rest of this lesson is learning to read and use that one sentence.
Why ignore constants? If one program takes steps and another takes , the first is five times slower, but a faster computer or a better compiler can make up that difference. What no hardware can fix is the shape of the growth. Double the input to an algorithm and it does four times the work, whatever the constant in front. Big-O keeps exactly that part and throws the rest away.
The hierarchy. Here are the common growth rates, from slowest-growing to fastest:
Each one eventually dwarfs everything to its left, no matter what constants you put in front. A algorithm loses to an one for small inputs, but once passes the one is behind for good.
Rules of thumb. These all follow from the definition: you're allowed to pick any constant , and you only care about large .
- Drop constant factors: .
- Keep the fastest-growing term: .
- Nested loops multiply: a loop of steps inside another of steps is .
- The base of a logarithm doesn't matter: and differ by a constant factor.
Related notation. Big-O is only an upper bound. is the matching lower bound (at least a constant times , eventually), and means both at once: grows exactly like .
A common mistake. Since is true, it's tempting to read as " grows like ." It doesn't say that. It only says grows no faster than . If you want to say an algorithm really takes about steps, the right symbol is .
Why it matters. At a billion operations per second, an algorithm on a million items takes about 17 minutes, an one about 0.02 seconds, and a one wouldn't finish before the stars burn out, even for .
See it
Worked examples
Show that .
- 1
The idea is to make every term look like a multiple of . For , and , so and .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Which grows fastest?
is
An algorithm takes 1 second for . About how many seconds does it take for ?
What is ?
A sort that takes steps handles 1 million records in 2 seconds. Estimate how long it takes for 8 million records, in seconds.
Is ?
The hardest one here. Get it, and you've got this lesson.
What is the smallest for which ?
Where it's used
Engineers choose algorithms by their growth rates, because at the scale of millions of users the gap between n log n and n² is the gap between instant and unusable.