When we compare algorithms, or counts like $2^{n}$ and $n!$, we care about what happens as $n$ grows, not about small cases or constant factors. Big-O notation makes that precise, and its definition is a nested quantifier statement. Logarithms, which grow slowly, sit at the bottom of the growth ladder.
Key idea
Why it works
| $n$ | $\log_{2} n$ | $n\log_{2} n$ | $n^{2}$ | $2^{n}$ |
|---|---|---|---|---|
| $10$ | $3.3$ | $33$ | $100$ | $1{,}024$ |
| $100$ | $6.6$ | $664$ | $10^{4}$ | $\approx 10^{30}$ |
| $1{,}000$ | $10.0$ | $9{,}966$ | $10^{6}$ | $\approx 10^{301}$ |
Worked examples
Worked example: Finding witnesses
- For $n \ge 1$: $5n \le 5n^{2}$ and $2 \le 2n^{2}$.
- So $f(n) \le 3n^{2} + 5n^{2} + 2n^{2} = 10n^{2}$.
- The witnesses $C = 10$, $k = 0$ (or $k = 1$) satisfy the definition.
Answer: $C = 10$ works for all $n \ge 1$.
Worked example: A constant cannot rescue it
- Suppose $n^{2} \le C \cdot 100n$ for all $n > k$. Dividing by $n$: $n \le 100C$ for all $n > k$.
- That fails for any $n$ larger than both $k$ and $100C$.
- So no $C$ works: $n^{2}$ is not $O(100n)$, even though $100n$ is the larger one for every $n < 100$.
Answer: No
Worked example: The tightest class
- The term $5n\log_{2} n$ eventually dominates: $100n \le 100\,n\log_{2} n$ once $\log_{2} n \ge 1$, and $7 \le 7n\log_{2} n$ too.
- So $f(n) \le 112\,n\log_{2} n$ for $n \ge 2$: $f$ is $O(n\log n)$. It is also $O(n^{2})$, but that is a looser statement. It is not $O(n)$, since $f(n)/n \ge 5\log_{2} n$ is unbounded.
Answer: $n\log n$
Common mistake