"There are maybe eight complexities you meet in real life. Learn the ladder once and you can place almost any algorithm on it at a glance."
The Ladder, Best to Worst
Ranked from "barely notices the input" to "dies if you sneeze at it":
- O(1) constant — dict lookup, array index, append to a list. The dream. Input size is irrelevant.
- O(log n) logarithmic — binary search, balanced-tree lookup. Halving each step. A billion items in ~30 steps.
- O(n) linear — scan a list, find a max. Touch each item once. Honest and fine.
- O(n log n) linearithmic — merge sort, Timsort, and the workhorse rung for processing large inputs intelligently. This is where serious general-purpose sorting lives.
- O(n²) quadratic — nested loops, naive sorts, all-pairs. Hundreds can be fine; millions are brutal. Check the actual input limit, but hear the alarm.
- O(n³) cubic — naive matrix multiply, triple-nested loops. Already rough at a few thousand.
- O(2ⁿ) exponential — try every subset, naive recursive Fibonacci. Dies around n = 40.
- O(n!) factorial — try every ordering, brute-force traveling salesman. Dies around n = 12.
Make the Gulf Visceral
The names hide how violent the differences are. At n = 50: O(n) is 50 operations, O(n²) is 2,500, O(2ⁿ) is about a quadrillion, and O(n!) is a number with 65 digits — more than the atoms in the observable universe. Same input. The algorithm's shape is the difference between "instant" and "the heat death of the universe finishes first." Run the code below and watch the numbers explode.
The Line You Don't Want to Cross
Constants, hardware, and latency limits set the exact boundary, but the ladder's alarm is still vivid. For everyday data, O(n log n) and below is usually "sleeps fine at night," while O(n²) is "fine until the data grows." O(2ⁿ) and O(n!) are fire alarms: they smile on tiny tests and detonate on real input. A huge part of algorithm design—especially the next Dynamic Programming track—is dragging exponential brute force down to a smaller state space or a polynomial-time method that can actually ship.