Sub topic 2
How Functions Grow: Comparing n, n Squared, and 2 to the n
This chapter assumes you can read basic algebra, including exponents, and that you have seen loops and arrays in code. Logarithms are explained briefly below. The previous chapter showed that we count operations as a function of input size n, not seconds. Here we compare how those counting functions behave as n gets large. Chapter 3 turns the comparison into the formal definition of Big O.
Why shape matters more than value
Suppose two algorithms solve the same problem. Algorithm A takes about n steps on an input of size n. Algorithm B takes about n² steps. At n = 10, A takes 10 steps and B takes 100. Both feel instant.
At n = 1,000,000, A takes a million steps. B takes a trillion. On a machine doing 10⁹ simple operations per second, A finishes in about a millisecond and B takes about 1,000 seconds, or 17 minutes.
The gap grows with n, so what matters is the growth rate: the shape of the function as n increases. A single measurement at one input size can hide it.
The functions to compare
These five functions cover most of what you will meet in practice:
- log n grows very slowly. Here log means log₂: the number of times you can halve n before reaching 1. For n = 1,024 that is 10.
- n is linear growth. Doubling the input doubles the cost.
- n log n is slightly worse than linear. It is common in efficient sorting.
- n² is quadratic growth. Doubling the input quadruples the cost.
- 2ⁿ is exponential growth. Adding one to the input doubles the cost.
Note the difference between n² and 2ⁿ. In n², the variable is the base and the exponent is fixed. In 2ⁿ, the base is fixed and the variable is the exponent. They look alike on paper but behave very differently.
A table of values
This script prints each function at four input sizes:
import math
functions = {
"log n": lambda n: math.log2(n),
"n": lambda n: n,
"n log n": lambda n: n * math.log2(n),
"n^2": lambda n: n ** 2,
"2^n": lambda n: 2 ** n,
}
sizes = [10, 20, 50, 100]
print(f"{'n':>5}", *[f"{name:>14}" for name in functions])
for n in sizes:
row = [f"{f(n):>14.4g}" for f in functions.values()]
print(f"{n:>5}", *row)The values it prints, rounded:
| n | log n | n | n log n | n² | 2ⁿ |
|---|---|---|---|---|---|
| 10 | 3.3 | 10 | 33 | 100 | 1,024 |
| 20 | 4.3 | 20 | 86 | 400 | about 1.05 million |
| 50 | 5.6 | 50 | 282 | 2,500 | about 1.1 × 10¹⁵ |
| 100 | 6.6 | 100 | 664 | 10,000 | about 1.3 × 10³⁰ |
Going from n = 10 to n = 100 multiplies n by 10 and n² by 100. It multiplies 2ⁿ by about 10²⁷. At 10⁹ operations per second, 2⁵⁰ steps take about 13 days. 2¹⁰⁰ steps take about 4 × 10¹³ years, which is thousands of times the age of the universe.
Seeing the curves
The chart plots n, n², and 2ⁿ for n from 1 to 10. The three lines start close together. By n = 10 the exponential has left the others behind.
At small n the order is not even stable. 2² = 4 equals 2², and at n = 3, n² = 9 is larger than 2³ = 8. They are equal again at n = 4 (both 16). From n = 5 on, 2ⁿ is always larger, and the gap widens. This is the main lesson of the chapter: comparisons at small n can mislead, and growth rates describe what happens as n becomes large.
The doubling test
A practical way to identify a growth rate is to compare the cost at n and at 2n. The ratio f(2n) / f(n) is a fingerprint:
import math
functions = {
"log n": lambda n: math.log2(n),
"n": lambda n: n,
"n log n": lambda n: n * math.log2(n),
"n^2": lambda n: n ** 2,
"2^n": lambda n: 2 ** n,
}
def ratio(f, n):
return f(2 * n) / f(n)
for name, f in functions.items():
print(f"{name:>8}: cost grows by x{ratio(f, 20):.2f} when n doubles from 20 to 40")The results:
| Function | Ratio when n doubles | What it means |
|---|---|---|
| log n | about 1.23 here, falling toward 1 | Adds a constant amount |
| n | 2.00 | Doubles |
| n log n | about 2.46 here, falling toward 2 | A little more than doubles |
| n² | 4.00 | Quadruples |
| 2ⁿ | 1,048,576 | Squares the cost, since 2²ⁿ = (2ⁿ)² |
The same test works on measurements. If you time a function at n and 2n and the time quadruples, quadratic growth is a good guess. If it multiplies by about 8, think cubic. This is an estimate, because real timings are noisy and carry constant overhead.
Polynomial versus exponential
Functions of the form nᵏ, for a fixed k, are polynomial. n, n², and n³ are all polynomial. Functions of the form cⁿ, with c greater than 1, are exponential. The key fact is that any exponential eventually outgrows any polynomial, no matter how large the polynomial's exponent.
That "eventually" can be far away. Compare n¹⁰⁰ with 2ⁿ. For modest n, n¹⁰⁰ is vastly larger. At n = 100, n¹⁰⁰ is 10²⁰⁰ while 2¹⁰⁰ is about 10³⁰. The exponential only passes it at a few hundred. Even so, it does pass it, and from then on the gap grows without bound.
This is why computer scientists draw a rough line between "tractable" polynomial-time algorithms and "intractable" exponential ones. In practice, polynomial algorithms with a high exponent are also unusable. Most useful algorithms have k of 3 or less.
Constants do not change the long-run order
Take 100n against n². At n = 10, the linear function costs 1,000 and the quadratic one costs 100, so the quadratic function is cheaper. They are equal at n = 100. Above that, n² is larger for every n.
A constant factor moves the crossover point. It cannot reverse the order for large enough n. This is the reason Big O ignores constant factors, which the next chapters formalize.
The same holds across the whole ranking. For large enough n:
- log n is smaller than n.
- n is smaller than n log n.
- n log n is smaller than n².
- n² is smaller than n³, and every polynomial is smaller than 2ⁿ.
- 2ⁿ is smaller than n!.
Common errors
- Reading n² as 2ⁿ, or the reverse. Check whether the variable sits in the base or in the exponent. For n = 20, n² is 400 while 2ⁿ is over a million.
- Judging by one small input. Both curves in the chart above cross at n = 4. Test a wide range of sizes, or reason about the formula.
- Forgetting the log base. log₂ n and log₁₀ n differ by a constant factor of about 3.32, so they have the same growth rate. In complexity analysis the base is usually not written.
- Assuming a faster computer fixes exponential growth. A machine 1,000 times faster lets a 2ⁿ algorithm handle only about 10 more input units, because 2¹⁰ is about 1,000. A quadratic algorithm handles about 32 times more input in the same time.
Putting numbers on feasibility
At 10⁹ operations per second, here is roughly how large n can be for a one-second time budget:
| Growth rate | Largest n in about 1 second |
|---|---|
| n | about 10⁹ |
| n log n | about 5 × 10⁷ |
| n² | about 30,000 |
| n³ | about 1,000 |
| 2ⁿ | about 30 |
These are rough upper limits. Real programs have overhead, so treat them as orders of magnitude. They show why the growth rate usually decides which algorithms are possible at a given input size.
Exercise
An algorithm takes 3 seconds on an input of size n = 1,000. Estimate its running time at n = 2,000 if its cost grows as (a) n, (b) n², and (c) 2ⁿ. Which of these can you not estimate sensibly, and why?
Show answer
(a) Linear: doubling n doubles the time, so about 6 seconds. (b) Quadratic: doubling n quadruples the time, so about 12 seconds. (c) Exponential: doubling n squares the cost. If the 1,000-size run took 3 seconds, the 2,000-size run would take about 3 × 2¹⁰⁰⁰ seconds, which is astronomically large (more than 10³⁰⁰ seconds). This is not a useful estimate in practice, and it shows that such an algorithm would not be usable at n = 1,000 in the first place.
Exercise
Find the smallest whole number n for which 2ⁿ is greater than 100n, and say whether it stays greater for all larger n.
Show answer
Check 100n against 2ⁿ. At n = 9, 2ⁿ = 512 and 100n = 900. At n = 10, 2ⁿ = 1,024 and 100n = 1,000. So n = 10 is the smallest value where 2ⁿ is greater. It stays greater for every larger n, because moving from n to n + 1 doubles 2ⁿ but adds only 100 to 100n.