Big-O Notation
Big-O notation describes how fast a function grows when its input gets large. We write f(x) = O(g(x)) when there are constants C and k with |f(x)| \le C|g(x)| for every x \gt k. Big-O ignores constant factors and smaller terms, so it is the standard way to compare the running times of algorithms.
Before this lesson: Predicates and Quantifiers, Logarithms and Limits at Infinity
The idea behind big-O
Suppose one algorithm takes 3n^2 + 5n + 7 steps on an input of size n. For a large n, the 3n^2 term is far bigger than the rest, and the factor 3 depends on the computer and the programmer. Big-O keeps only what matters for large inputs: the steps grow like n^2.
Big-O is an upper bound. Saying f(x) = O(g(x)) means that, past some point, f is never more than a fixed multiple of g. The “=” is a convention; it is read “f is big-O of g” and means f belongs to the set of functions that grow no faster than g.
Definition of big-O notation
Let f and g be functions from the real numbers (or the positive integers) to the real numbers.
f(x) = O(g(x)) \iff \text{there are constants } C \gt 0 \text{ and } k \text{ such that } |f(x)| \le C|g(x)| \text{ for all } x \gt kThe constants C and k are called witnesses. To prove f(x) = O(g(x)), find one pair of witnesses that works for every x \gt k. To prove that f is not O(g(x)), show that no pair can work, usually by contradiction.
Rules that make big-O quick
- Constants drop: c \cdot f(x) is O(f(x)) for any constant c.
- Polynomials: a polynomial of degree d is O(x^d).
- Sums: if f_1 = O(g_1) and f_2 = O(g_2), then f_1 + f_2 = O(\max(|g_1|, |g_2|)); the faster-growing term wins.
- Products: if f_1 = O(g_1) and f_2 = O(g_2), then f_1 f_2 = O(g_1 g_2).
- Log bases don’t matter: \log_a n = \frac{\log_b n}{\log_b a}, a constant multiple of \log_b n, so we just write O(\log n).
Common growth rates
From slowest to fastest, these are the growth rates you will meet most often. Each one is big-O of every function to its right, but not of any function to its left.
| Name | Function | n = 10 | n = 20 | Typical algorithm |
|---|---|---|---|---|
| Constant | 1 | 1 | 1 | Look up an array entry |
| Logarithmic | \log_2 n | 3.32 | 4.32 | Binary search |
| Linear | n | 10 | 20 | Find the largest of n numbers |
| Linearithmic | n\log_2 n | 33.2 | 86.4 | Merge sort |
| Quadratic | n^2 | 100 | 400 | Compare every pair |
| Cubic | n^3 | 1,000 | 8,000 | Multiply two n × n matrices directly |
| Exponential | 2^n | 1,024 | 1,048,576 | Check every subset |
| Factorial | n! | 3,628,800 | about 2.4 \times 10^{18} | Check every ordering |
Big-O notation examples
Example 1: Find witnesses for a polynomial
Show that f(x) = 3x^2 + 5x + 7 is O(x^2).
3x^2 + 5x + 7
Take x > 1, so every term is positive and |f(x)| = f(x).
\le 3x^2 + 5x^2 + 7
For x > 1, x \lt x^2, so 5x \le 5x^2.
\le 3x^2 + 5x^2 + 7x^2
For x > 1, 1 \lt x^2, so 7 \le 7x^2.
= 15x^2
Combine like terms.
Answer: |3x^2 + 5x + 7| \le 15|x^2| for all x \gt 1, so f(x) = O(x^2) with witnesses C = 15 and k = 1. Other witnesses also work, such as C = 4 and k = 7, but one pair is enough.
Example 2: Show that a function is not big-O
Show that x^2 is not O(x).
x^2 \le Cx \text{ for all } x \gt kSuppose, for contradiction, that witnesses C and k exist (see proof by contradiction); for positive x the absolute values can be dropped.
x \le C \text{ for all } x \gt k,\ x \gt 0Divide both sides by the positive number x.
x = \max(k, C) + 1
Choose this x. It is greater than k and positive.
\max(k, C) + 1 \le C
The inequality must hold for this x.
\max(k, C) + 1 \gt C
But \max(k, C) \ge C, so the left side is greater than C, a contradiction.
Answer: no witnesses can exist, so x^2 is not O(x). A function that grows like x^2 cannot be held under any fixed multiple of x.
Example 3: Rank functions by growth
Arrange 100n, n\log_2 n, n^3, 2^n and 5\log_2 n so that each is big-O of the next.
5\log_2 n \le 5n \text{ for } n \ge 1\log_2 n \lt n for every n ≥ 1, so 5\log_2 n = O(n) and also O(100n).
100n \le 100\,n\log_2 n \text{ for } n \ge 2\log_2 n \ge 1 when n ≥ 2, so 100n = O(n\log_2 n) with C = 100.
n\log_2 n \le n \cdot n^2 = n^3 \text{ for } n \ge 1\log_2 n \lt n \le n^2, so n\log_2 n = O(n^3) with C = 1.
n^3 \le 2^n \text{ for } n \gt 9Check n = 10: 10^3 = 1000 \le 1024 = 2^{10}. Each later step multiplies 2^n by 2 but n^3 by \left(\frac{n + 1}{n}\right)^3 \le 1.331, so the inequality stays true and n^3 = O(2^n) with C = 1, k = 9.
Answer: 5\log_2 n, 100n, n\log_2 n, n^3, 2^n. Note that 100n is larger than n\log_2 n until \log_2 n passes 100, near n = 2^{100}; big-O only describes what happens eventually.
Example 4: Count the steps of nested loops
An algorithm runs “for i = 1 to n: for j = 1 to i: compare two items.” Count the comparisons and give a big-O estimate.
1 + 2 + 3 + \cdots + n
The inner loop runs i times on the i-th pass of the outer loop.
\frac{n(n + 1)}{2}Sum the series (see arithmetic series).
\frac{n^2}{2} + \frac{n}{2}Distribute and split the fraction.
\le \frac{n^2}{2} + \frac{n^2}{2}For n ≥ 1, n \le n^2.
= n^2
Add the two halves.
Answer: the algorithm makes \frac{n(n + 1)}{2} comparisons, which is O(n^2) with witnesses C = 1 and k = 1. Doubling n roughly quadruples the work.
Example 5: Choosing between two algorithms (word problem)
Algorithm A sorts n records in 50n\log_2 n steps, and algorithm B sorts them in 2n^2 steps. Which is faster for 100 records? For 1,000 records? Which has the better big-O?
50(100)\log_2 100 \approx 50(100)(6.644)
A at n = 100; \log_2 100 \approx 6.644.
\approx 33{,}220Multiply: A takes about 33,220 steps.
2(100)^2 = 2(10{,}000) = 20{,}000B at n = 100.
50(1000)\log_2 1000 \approx 50(1000)(9.966)
A at n = 1,000; \log_2 1000 \approx 9.966.
\approx 498{,}300Multiply: A takes about 498,300 steps.
2(1000)^2 = 2(1{,}000{,}000) = 2{,}000{,}000B at n = 1,000.
Answer: B is faster for 100 records (20,000 steps against about 33,220), but A is faster for 1,000 records (about 498,300 against 2,000,000), roughly 4 times faster. A is O(n\log n) and B is O(n^2), so A has the better big-O, and its advantage keeps growing; the two cross near n = 190.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Reading big-O as “equals” | Saying n is not O(n^2) because n is not n^2 | Big-O is an upper bound: n is O(n^2), though O(n) is the tighter, more useful estimate. |
| Keeping constant factors | Writing O(3n^2) | Drop constants: O(n^2). |
| Keeping smaller terms | Writing O(n^2 + n\log n) | Keep only the fastest-growing term: O(n^2). |
| Judging growth at a small n | Deciding 100n grows faster than n^2 because 1,000 > 100 at n = 10 | Big-O describes large n: n^2 catches 100n at n = 100 and is ahead for every n after that. |
| Checking witnesses at only one point | Showing 3x^2 + 5x + 7 \le 15x^2 only at x = 2 | The inequality must hold for every x > k; prove it with inequalities that hold for all such x. |
| Reversing the bound | Claiming n^2 = O(n) because both grow | n^2 is not O(n) (Example 2); the bound runs from slower to faster. |
Related ideas
Big-Omega and big-Theta
Big-Omega is a lower bound: f(x) = \Omega(g(x)) when |f(x)| \ge C|g(x)| for all x \gt k, with C > 0. Big-Theta is both: f(x) = \Theta(g(x)) when f is O(g(x)) and \Omega(g(x)), so f and g grow at the same rate. The count in Example 4 is \Theta(n^2).
A limit test
If \lim_{x \to \infty} \frac{|f(x)|}{|g(x)|} is a finite number, then f(x) = O(g(x)). If the limit is \infty, then f is not O(g(x)). This turns many big-O questions into limits at infinity.
Worst case, average case
Running times are usually stated for the worst case, the input of size n that takes the most steps. An algorithm can also be described by its average case, but the big-O idea is the same.
Practice problems
- Find witnesses C and k to show that 4x + 9 is O(x).
- Show that 2x^3 + x^2 + 6 is O(x^3).
- Give a big-O estimate for (n^2 + 1)(3n + 2).
- Give the simplest big-O estimate for 8n^2 + 100n\log_2 n + 5000.
- Show that n^3 is not O(n^2).
- Is 2^{n + 1} big-O of 2^n? Give witnesses or explain why not.
- Is 3^n big-O of 2^n? Explain.
- Arrange n^2, \log_2 n, n!, \sqrt{n} and 3^n from slowest-growing to fastest-growing.
- An algorithm has three nested loops, each running from 1 to n, with one operation inside. Give a big-O estimate, and count the operations when n = 100.
- A computer does 10^9 operations per second. How long does an algorithm that takes 2^n operations need when n = 50? How long does one that takes n^2 operations need?
Show answers
1.
4x + 9 \le 4x + 9x
For x > 1, 9 \lt 9x.
= 13x
Combine like terms.
Answer: C = 13 and k = 1 work, since |4x + 9| \le 13|x| for x > 1
2.
2x^3 + x^2 + 6 \le 2x^3 + x^3 + 6
For x > 1, x^2 \lt x^3.
\le 2x^3 + x^3 + 6x^3
For x > 1, 1 \lt x^3, so 6 \le 6x^3.
= 9x^3
Combine like terms.
Answer: witnesses C = 9, k = 1
3.
3n^3 + 2n^2 + 3n + 2
Multiply the two factors (see multiplying polynomials).
O(n^3)
A polynomial of degree 3 is O(n^3).
Answer: O(n^3)
4.
O(n^2) + O(n\log n) + O(1)
Estimate each term, dropping the constants.
O(n^2)
Keep the fastest-growing term: n\log n \le n \cdot n = n^2.
Answer: O(n^2)
5.
n^3 \le Cn^2 \text{ for all } n \gt kSuppose witnesses C and k exist.
n \le C \text{ for all } n \gt kDivide both sides by the positive number n^2.
n = \max(k, C) + 1 \gt C
This n is greater than k but also greater than C, a contradiction.
Answer: no witnesses exist, so n^3 is not O(n^2)
6.
2^{n + 1} = 2 \cdot 2^nProduct of powers (see exponent rules).
\le 2 \cdot 2^n \text{ for all } nThe inequality holds with equality.
Answer: yes, with witnesses C = 2 and k = 0
7.
3^n \le C \cdot 2^n
Suppose witnesses C and k exist, for all n > k.
1.5^n \le C
Divide both sides by 2^n; \frac{3^n}{2^n} = 1.5^n.
1.5^n \to \infty
Since 1.5 > 1, 1.5^n grows without bound, so it cannot stay at most C.
Answer: no; 3^n grows faster than any constant multiple of 2^n
8.
\log_2 n,\ \sqrt{n},\ n^2Logarithms grow slower than any power of n, and \sqrt{n} = n^{1/2} is a lower power than n^2.
n^2,\ 3^n,\ n!
Exponentials beat polynomials, and n! beats 3^n (each new factor of n! is eventually bigger than 3).
Answer: \log_2 n, \sqrt{n}, n^2, 3^n, n!
9.
n \cdot n \cdot n = n^3
Each loop runs n times, and the loops are nested, so multiply.
100^3 = 1{,}000{,}000Substitute n = 100.
Answer: O(n^3); 1,000,000 operations when n = 100
10.
2^{50} = 1{,}125{,}899{,}906{,}842{,}624Number of operations for 2^n.
\frac{1{,}125{,}899{,}906{,}842{,}624}{10^9} \approx 1{,}125{,}900 \text{ s}Divide by the operations per second.
\frac{1{,}125{,}900}{86{,}400} \approx 13.03 \text{ days}Divide by 86,400 seconds per day.
50^2 = 2500
Number of operations for n^2.
\frac{2500}{10^9} = 0.0000025 \text{ s}Divide by the operations per second.
Answer: about 13 days for 2^n, but only 2.5 millionths of a second for n^2
Big-O notation FAQ
What does O(n) mean?
It means the running time grows at most linearly: past some input size, it is never more than a fixed multiple of n. Doubling the input roughly doubles the work, at worst.
What is the difference between big-O, big-Omega and big-Theta?
Big-O is an upper bound, big-Omega is a lower bound, and big-Theta means both, so the two functions grow at the same rate. In everyday use, people often say big-O when they mean the tight big-Theta bound.
Is big-O the worst case?
Not by definition. Big-O is a bound on a function, and you can apply it to the worst-case, average-case or best-case running time. Most running times quoted for algorithms are worst-case bounds.
What is the order of common big-O complexities?
From fastest algorithm to slowest: O(1), O(\log n), O(n), O(n\log n), O(n^2), O(n^3), O(2^n) and O(n!).
Summary
- f(x) = O(g(x)) means |f(x)| \le C|g(x)| for all x \gt k, for some witnesses C and k.
- Drop constant factors and smaller terms; a degree-d polynomial is O(x^d).
- To prove a big-O bound, find witnesses; to disprove one, show by contradiction that none exist.
- Growth order: 1, \log n, n, n\log n, n^2, n^3, 2^n, n!.
- Big-O describes large inputs; a better big-O can lose for small n but always wins eventually.
Previous lesson: Logic Gates
Next lesson: Cost, Revenue and Profit Functions, the first lesson of Finite Math and Business Calculus. See the full Finite Math and Business Calculus Index.
Back to the Discrete Math Index