How an Algorithm Scales
Two programs can both be correct yet differ wildly in speed as data grows. Complexity analysis measures how an algorithm's cost grows with input size , ignoring hardware constants to focus on the growth shape.
Big-O notation expresses an upper bound on that growth. For large , growth classes dominate all constants; an sort beats regardless of tuning.
| Big-O | Name | Example |
|---|---|---|
| Constant | Array index access | |
| Logarithmic | Binary search (halving step) | |
| Linear | Scanning a list once | |
| Linearithmic | Efficient sorting | |
| Quadratic | Nested loops over data | |
| Exponential | Trying all subsets |
Trade-offs and Pitfalls
Performance involves choices beyond raw loops. The space-time trade-off lets you spend more memory to save time, such as using a lookup table.
An algorithm also has best, average, and worst cases. Big-O usually describes the worst case unless specified otherwise.
Common pitfall: Judging performance on small inputs where constant factors dominate. An algorithm can beat an one on tiny inputs, yet lose catastrophically as scales.