Big O Notation: O, Omega, Theta and the Common Growth Classes
Big O notation describes how the cost of an algorithm grows as its input grows. It does not tell you how many milliseconds a program takes; it tells you the shape of the curve. A linear-time algorithm does roughly twice the work when the input doubles; a quadratic-time algorithm does roughly four times the work. This guide explains what n is, how to count, what O, Omega and Theta formally mean, and the handful of growth classes you will meet everywhere.
What n is
n is the size of the input, in whatever unit makes sense: the number of items in a list, the number of characters in a string, the number of vertices in a graph (graphs often use two sizes, V vertices and E edges). When an algorithm has two independent inputs, keep two variables: comparing two strings of lengths m and n is often O(mn), and writing it as O(n^2) loses information.
Counting basic operations
To analyze an algorithm, pick a basic operation that takes constant time (a comparison, an addition, an array access) and count how many times it runs as a function of n. Consider this function, which looks at every ordered pair of positions:
- the outer loop runs n times;
- for each of those, the inner loop runs n times;
- so the body runs n x n = n^2 times.
Real code also has setup, a few assignments per iteration and a return. A more honest count might be 3n^2 + 5n + 2. Big O keeps only the part that matters for large n.
Dropping constants and lower-order terms
3n^2 + 5n + 2 is O(n^2). Two rules get you there:
- Drop lower-order terms. As n grows, 5n + 2 becomes negligible next to 3n^2. At n = 1000 the n^2 term contributes 3,000,000 and the rest contributes 5,002.
- Drop constant factors. The 3 depends on the machine, the language and how you count. It does not change the growth rate, so it is left out.
Constants still matter in practice. An algorithm with cost 100n is slower than one with cost n^2 for every n below 100. Big O is a statement about large inputs, not a ranking at every size.
O, Omega and Theta
The three symbols are bounds, defined with two constants: a multiplier c > 0 and a threshold n0 beyond which the inequality must hold.
- O (upper bound). f(n) is O(g(n)) if f(n) <= c x g(n) for all n >= n0. Example: 3n^2 + 5n + 2 <= 10n^2 for every n >= 1, so it is O(n^2) with c = 10 and n0 = 1.
- Omega (lower bound). f(n) is Omega(g(n)) if f(n) >= c x g(n) for all n >= n0. Here 3n^2 + 5n + 2 >= 3n^2, so it is Omega(n^2).
- Theta (tight bound). f(n) is Theta(g(n)) if it is both O(g(n)) and Omega(g(n)). Our function is Theta(n^2).
Because O is only an upper bound, it is technically true that 3n^2 + 5n + 2 is O(n^3), or O(2^n). Those statements are correct and useless. In everyday speech, and in most interview and textbook discussions, people say "Big O" and mean the tight bound: "binary search is O(log n)" is meant as "it is Theta(log n) in the worst case". When precision matters, use Theta for tight and keep O for "no worse than".
These bounds describe functions, not algorithms. To use them you first decide which function you are bounding: the worst-case running time, the average, or the best. That choice is the subject of the best, average and worst case guide.
The common growth classes
The counts below come from the Big O Complexity Analyzer, which evaluates one representative function per class (1, log2 n, n, n log2 n, n^2, n^3, 2^n, n!) at the n you choose. They are values of those formulas, not instruction counts or times, and they are rounded to two decimals.
| Class | Name | Typical example | n = 10 | n = 1000 |
|---|---|---|---|---|
| O(1) | constant | reading items[i] from a list | 1 | 1 |
| O(log n) | logarithmic | binary search in a sorted list | 3.32 | 9.97 |
| O(n) | linear | scanning a list once | 10 | 1,000 |
| O(n log n) | linearithmic | merge sort | 33.22 | 9,965.78 |
| O(n^2) | quadratic | comparing every pair of items | 100 | 1,000,000 |
| O(n^3) | cubic | multiplying two n by n matrices with three nested loops | 1,000 | 1,000,000,000 |
| O(2^n) | exponential | trying every subset of n items | 1,024 | about 1.07 x 10^301 |
| O(n!) | factorial | trying every ordering of n items | 3,628,800 | a 2,568-digit number |
The tool displays 2^1000 as 1.07e+301 and 1000! as "∞", because 1000! is too large for a JavaScript number; the digit count in the table was computed separately with Python's math.factorial. The jump between rows is the whole point: at n = 10 everything is small, and at n = 1000 the bottom two rows are out of reach for any computer. One quirk if you try the tool: at n = 1, log2 n is 0, so it ranks O(log n) and O(n log n) below O(1). Use n of 2 or more.
Rules for loops
- Sequential blocks add. A loop of n steps followed by another loop of n steps is n + n = 2n, which is O(n). A loop of n followed by a nested n by n loop is O(n + n^2) = O(n^2): the larger term wins.
- Nested loops multiply. An outer loop of n with an inner loop of m is O(nm). If the inner loop runs from i + 1 to n, the count is n(n - 1)/2, still O(n^2): halving the work is a constant factor.
- A halving loop is logarithmic. A loop that divides n by 2 each pass runs about log2 n times before n reaches 1. Binary search is this shape.
- Function calls count. A loop that calls an O(n) function on each of n items is O(n^2), even if the loop looks like a single line. In Python,
x in some_listinside a loop is the classic hidden case.
Examples use Python 3.10 or later and only the standard library. This script counts the inner-loop steps of three loop shapes for a few values of n.
def count_pair_steps(n):
steps = 0
for i in range(n):
for j in range(n):
steps += 1
return steps
def count_triangle_steps(n):
steps = 0
for i in range(n):
for j in range(i + 1, n):
steps += 1
return steps
def count_halving_steps(n):
steps = 0
while n > 1:
n //= 2
steps += 1
return steps
for n in [10, 100, 1000]:
print(n, count_pair_steps(n), count_triangle_steps(n), count_halving_steps(n))
Output (columns: n, all pairs, pairs with j > i, halvings):
10 100 45 3
100 10000 4950 6
1000 1000000 499500 9
Each tenfold increase in n multiplies the first column by 100, the second by roughly 100 (it is exactly n(n - 1)/2), and adds only about 3 to the third.
The base of the logarithm does not matter
log2 n and log10 n differ only by a constant factor: log2 n = log10 n / log10 2, and 1 / log10 2 is about 3.32. Since O ignores constant factors, O(log2 n), O(log10 n) and O(ln n) are the same class, and people write O(log n). The base does matter inside an exponent: 2^n and 3^n are different classes, because 3^n / 2^n = 1.5^n is not a constant.
Comparing algorithms with these classes
The Algorithm Comparison Tool applies the same functions to a fixed table of eight textbook algorithms. At n = 1000 in the average case it ranks binary search at 9.97, linear search at 1,000, merge sort, quicksort and heap sort at 9,965.78, and bubble sort at 1,000,000. Those are the formulas for each algorithm's stated class evaluated at n = 1000, which makes them a picture of growth, not a measurement.
Check yourself
- What is the Big O of 7n log n + 2n + 40? (O(n log n).)
- A loop runs n times, and inside it a loop doubles a counter from 1 until it exceeds n. What is the total? (O(n log n).)
- Is it true that every O(n) algorithm is also O(n^2)? (Yes, as an upper bound. It is not Theta(n^2).)