How Big-O notation works
Big-O notation describes how the work an algorithm does grows as its input grows. It ignores the speed of the computer and the details of the code and keeps only the shape of the growth, which is what decides whether a program still finishes when the data gets a thousand times bigger. Here is what the common classes mean and how to tell which one an algorithm belongs to.
What Big-O measures
Big-O counts the basic steps an algorithm takes, such as comparisons, additions or memory reads, as a function of the input size n, and keeps only the fastest-growing term without its constant factor. A loop that does 5 operations for each of n items, plus 20 for setup, takes 5n + 20 steps and is O(n). Formally, f(n) is O(g(n)) when f(n) ≤ c · g(n) for some constant c and every n beyond some point, so Big-O is an upper bound on growth.
The common classes
O(1), constant: the work does not depend on n, like reading an array element by its index. O(log n), logarithmic: each step halves the problem, like binary search, which finds an item among a million in 20 comparisons. O(n), linear: each item is looked at once. O(n log n): the best a sort that compares items can do, reached by merge sort and heap sort. O(n²) and O(n³), polynomial: loops nested two or three deep over the input. O(2ⁿ), exponential: trying every subset. O(n!), factorial: trying every order.
Why the class matters more than the computer
A computer a thousand times faster lets an O(n) algorithm handle a thousand times more data in the same time, an O(n²) one about 32 times more, and an O(2ⁿ) one only about 10 more items, since 2¹⁰ ≈ 1,000. Going from an O(n²) to an O(n log n) algorithm turns a sort of a million items from about 10¹² steps into about 2 × 10⁷: from many minutes to a fraction of a second. No hardware can rescue an exponential algorithm on large inputs.
Finding the complexity of code
Add the costs of parts that run one after the other, multiply the costs of loops inside loops, then keep the largest term. A loop over n items inside another such loop is O(n²). A loop that halves its range each time is O(log n). A recursion that splits the input into two halves and does linear work to combine them, like merge sort, is O(n log n). A recursion that calls itself twice on n − 1, like the naive Fibonacci function, takes exponential time, about 1.6ⁿ calls.
Worst case, average case and the other symbols
Big-O usually describes the worst case: quicksort is O(n²) in the worst case, though O(n log n) on average. Big-Omega, Ω, is a lower bound, and Big-Theta, Θ, means both at once: merge sort is Θ(n log n). The same notation describes memory: merge sort needs O(n) extra memory, heap sort O(1). Amortised analysis averages over a sequence of operations, which is why appending to a dynamic array counts as O(1) even though it sometimes copies everything.
Frequently asked questions
Is O(log n) the same in every base?
Yes. Logarithms in different bases differ only by a constant factor: log₂ n = 3.32 × log₁₀ n. Big-O ignores constant factors, so O(log₂ n), O(log₁₀ n) and O(ln n) are all simply O(log n).
Does a better Big-O always mean faster code?
Only for large enough inputs. Constant factors, memory access patterns and the size of real inputs matter too: insertion sort, O(n²), beats merge sort on a few dozen items, and a hash table, O(1) on average, can lose to a linear search through a tiny array. Big-O tells you what happens as n grows.
What are P and NP?
P is the class of problems a computer can solve in polynomial time, O(nᵏ) for some fixed k. NP is the class whose solutions can be checked in polynomial time. Many NP problems, such as the travelling salesman problem, have no known polynomial algorithm, so the exact methods take exponential or factorial time. Whether P equals NP is one of the great open questions of mathematics.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open the Big-O Comparison