Almost every algorithm you encounter falls into one of a small set of complexity classes. Recognizing them is the fastest way to estimate performance.
The hierarchy
From fastest to slowest: .
What each one means at scale
For , is ~1 ms, is ~20 ms, is unrunnable (~1000 seconds), and is impossible for .
Examples per class
- : array indexing, hash map lookup, push/pop on a stack
- : binary search, balanced-tree operations, heap insert/extract
- : linear scan, single-pass aggregations, BFS/DFS over a sparse graph
- : comparison-based sorting (merge sort, heap sort), divide-and-conquer that touches each element
- : nested loops over pairs, naive sorting, Floyd-Warshall on a small graph
- : enumerating subsets, naive recursive Fibonacci
- : enumerating permutations, brute-force TSP
Practical takeaway
If you're handed a -element input, you have time for comfortably and only barely. At , is your ceiling. At , you need linear or sublinear. Knowing the input size and target class tells you which algorithm family to reach for before you write a single line.