The ladder of growth rates
Constant, logarithmic, linear, n log n, polynomial, exponential and factorial, plus the two odd ones that turn up in real analysis.
Almost every running time you meet falls into a short list of shapes, and knowing that list by heart is what lets you glance at an algorithm and know whether it is worth writing. The differences between the shapes are much larger than most people expect, so it is worth watching them once at full scale.
The following table prints, for a few input sizes, what each shape evaluates to. Read the last two columns carefully.
Add 100 to the list and run it again. Count the digits in the last two entries of that row.
At the quadratic column is 3600, which any machine finishes instantly. The column is about , which at a billion operations a second takes about 36 years. The factorial column is about , which is a number of the same order as the count of atoms in the observable universe.
The classes, in order
| Growth | Name | Where it comes from | Largest n that is comfortable |
|---|---|---|---|
| Theta(1) | constant | a fixed amount of work, whatever the input | any |
| Theta(log n) | logarithmic | halving the problem a few times | any |
| Theta(sqrt n) | root | trying divisors up to the square root of a number | about 10^16 |
| Theta(n) | linear | one pass over the input | about 10^8 |
| Theta(n log n) | linearithmic | a pass for each of log n rounds | about 10^6 to 10^7 |
| Theta(n^2) | quadratic | every pair of items | about 10^4 |
| Theta(n^3) | cubic | every triple, or multiplying matrices the obvious way | about 500 |
| Theta(2^n) | exponential | every subset of n items | about 25 |
| Theta(n!) | factorial | every ordering of n items | about 11 |
A polynomial cost is one bounded by for some fixed : linear, quadratic and cubic are all polynomial. An exponential cost has in the exponent, like or . Every polynomial is eventually beaten by every exponential, however extreme the exponents look: is smaller than for large enough , though that is astronomically large.
Why the base of a logarithm is not written
A logarithm answers the question: to what power must the base be raised to reach this number? Since , . Changing base only multiplies by a fixed number, because , and does not depend on .
So and differ by the constant factor , and constants vanish inside Theta. That is why is written with no base at all. The one place the base does matter is inside an exponent: and are genuinely different, since their ratio grows without bound.
Add math.log(n), which is the natural logarithm, as a fourth column. Its ratio to log2 is another constant. Which one?
Harmonic sums
One pattern turns up so often that its answer is worth knowing outright. A loop whose inner work shrinks as , then , then , and so on, adds up to times the harmonic number .
The difference settles on a number near 0.577. Push the loop to ten million and watch how little it moves.
The sum of a million reciprocals is only about 14.39, and it stays about plus a fixed number close to 0.5772. So , and a loop costing costs , not as the first term alone might suggest.
The iterated logarithm
The iterated logarithm, written , counts how many times you must take the logarithm before the result drops to 1 or below. It grows so slowly that for every input any real computer will ever hold, its value is at most 5.
Add 65536 and 65537 to the list and run it again. The answer climbs from 4 to 5 between those two, and it does not reach 6 until n passes 2 to the power 65536, a number with about twenty thousand digits.
Put a million items through each of those two shapes and count. A linear pass is operations, which at a hundred million operations a second is about a hundredth of a second. A quadratic one is operations, which at the same rate is seconds, or a little under three hours.
A loop runs n times, and on pass number i it does i units of work. What is the total, and how does it compare with a loop that does n units of work on every pass?
The total is 1 + 2 + ... + n = n(n+1)/2, which is Theta(n squared). The comparison loop does n times n = n squared units, also Theta(n squared). They differ by a factor of about 2, which asymptotic notation deliberately discards. Contrast this with the harmonic case, where pass i does n/i units: that total is Theta(n log n), a genuinely different class, because the work shrinks fast enough to change the shape rather than only the constant.