Big O notation in plain terms: what it tells you and what it doesn't
Big O describes how an algorithm's work grows as the input grows. It's a way to compare approaches without measuring every case.
Common complexities, from fast to slow
- O(1), constant: looking up an item in an array by index, or a hash table on average.
- O(log n), logarithmic: binary search; doubling the input adds one step.
- O(n), linear: looping through a list once.
- O(n log n): efficient general-purpose sorting.
- O(n²), quadratic: comparing every item with every other item, like a nested loop over the same list.
- O(2ⁿ), exponential: trying every subset; becomes impractical very quickly.
What it tells you
How cost scales. An O(n²) approach that's fine for 100 items may be painfully slow for 100,000, while an O(n log n) one stays manageable.
What it doesn't tell you
- Constant factors. An O(n) algorithm doing heavy work per item can be slower than an O(n log n) one for realistic sizes.
- Small inputs. For tiny data, the "worse" algorithm is often faster and simpler.
- Hardware effects. Cache-friendly memory access can matter more than the theoretical complexity.
- Average vs worst case. Hash tables are O(1) on average but can degrade.
- Memory use, which has its own complexity.
Practical use
Spotting accidental quadratic behavior, like searching a list inside a loop when a set lookup would do, is the most common real-world win.
Where has thinking in Big O helped you most? Have you ever chosen a "slower" algorithm because it was faster in practice?
Comments · 4
The nested loop example made it click. Two loops over the same list means roughly n squared steps, so doubling input quadruples the work.
Most of the time, readability beats a better Big O on small inputs. Profile before optimizing.
Does Big O ignore constant factors even when they're huge? Seems like it could mislead in practice.
It does. Big O ignores constants, so a "slower" algorithm can win for small inputs. Measure real data when it matters.