Skip to content
Part IA Lent Term

Comparing Growth Rates

Intuition by Algorithmic Pattern

Growth rateTypical algorithm pattern
O(1)O(1)Array access, arithmetic, hash table lookup
O(logn)O(\log n)Binary search, balanced BST operations, divide-and-conquer depth
O(n)O(n)Linear scan, counting sort, merging two sorted lists
O(nlogn)O(n \log n)MergeSort, HeapSort, QuickSort average case, FFT
O(n2)O(n^2)Insertion Sort, nested pairwise loops, naive matrix multiplication
O(n3)O(n^3)Triple-nested loops, Floyd-Warshall all-pairs shortest path
O(2n)O(2^n)Brute-force subset enumeration, naive recursion for Fibonacci
O(n!)O(n!)Brute-force permutation enumeration, travelling salesman (naive)

Concrete Numbers

For a machine doing 10910^9 operations per second:

nnlog2n\log_2 nnnnlog2nn \log_2 nn2n^2n3n^32n2^n
103.310 ns33 ns100 ns1 µs1 µs
1006.6100 ns660 ns10 µs1 ms4×10134 \times 10^{13} years
1,000101 µs10 µs1 ms1 sastronomically large
10610^6201 ms20 ms17 min32,000 yearsimpossible
10910^9301 s30 s32,000 yearsimpossibleimpossible

A supercomputer running at 101510^{15} ops/sec would handle n=106n=10^6 with Insertion Sort (n2n^2) in about 1 second, and MergeSort (nlognn \log n) instantly. The algorithm matters far more than the hardware.

Using Limits to Compare Growth

To determine the asymptotic relationship between f(n)f(n) and g(n)g(n), compute:

limnf(n)g(n)\lim_{n \to \infty} \frac{f(n)}{g(n)}

LimitConclusion
0f(n)=o(g(n))f(n) = o(g(n)), so f(n)=O(g(n))f(n) = O(g(n)) but not Θ(g(n))\Theta(g(n))
c(0,)c \in (0, \infty)f(n)=Θ(g(n))f(n) = \Theta(g(n))
\inftyf(n)=ω(g(n))f(n) = \omega(g(n)), so f(n)=Ω(g(n))f(n) = \Omega(g(n)) but not Θ(g(n))\Theta(g(n))

Example: Compare nlognn \log n and n2n^2

limnnlognn2=limnlognn=0(by L’Hoˆpital)\lim_{n \to \infty} \frac{n \log n}{n^2} = \lim_{n \to \infty} \frac{\log n}{n} = 0 \quad \text{(by L'Hôpital)}

So nlogn=o(n2)n \log n = o(n^2); the n2n^2 function dominates.

Example: Compare 3n2+2n+13n^2 + 2n + 1 and n2n^2

limn3n2+2n+1n2=3\lim_{n \to \infty} \frac{3n^2 + 2n + 1}{n^2} = 3

The limit is finite and non-zero, so 3n2+2n+1=Θ(n2)3n^2 + 2n + 1 = \Theta(n^2).

L’Hôpital’s Rule

If limf(n)=limg(n)=\lim f(n) = \lim g(n) = \infty, then:

limf(n)g(n)=limf(n)g(n)\lim \frac{f(n)}{g(n)} = \lim \frac{f'(n)}{g'(n)}

This is essential for ratios like lognn\frac{\log n}{n} or nlogn\frac{n}{\log n} where the asymptotic behaviour is not obvious from inspection.

Practice: Rank by Growth Rate

Rank these functions from slowest to fastest growth:

2log3n,n2,n,log(n!),2n,nlog3,nlogn,(logn)logn2^{\log_3 n}, \quad n^2, \quad \sqrt{n}, \quad \log(n!), \quad 2^n, \quad n^{\log 3}, \quad n \log n, \quad (\log n)^{\log n}

Solution: 2log3n=nlog32n0.632^{\log_3 n} = n^{\log_3 2} \approx n^{0.63}; log(n!)=Θ(nlogn)\log(n!) = \Theta(n \log n); nlog3n1.59n^{\log 3} \approx n^{1.59}. The rank: n<2log3n<nlognlog(n!)<nlog3<n2<(logn)logn<2n\sqrt{n} < 2^{\log_3 n} < n \log n \approx \log(n!) < n^{\log 3} < n^2 < (\log n)^{\log n} < 2^n.

Summary

RateExample algorithmn=106n=10^6 on 1 GHz
logn\log nBinary search20 ns
nnLinear scan1 ms
nlognn \log nMergeSort20 ms
n2n^2Insertion Sort worst17 min
n3n^3Floyd-Warshall32,000 years
2n2^nSubset enumerationimpossible
Limit testlimf/g\lim f/g determines oo, Θ\Theta, ω\omega