How we measure cost
Big-O: how work grows with input size n —
the measuring stick for the whole book.
Count operations, not seconds
- Constant O(1) — fixed work, any n
- Linear O(n) — one pass
- Logarithmic O(logn) — halve the input each step
- Keep the dominant term, drop the constants
The ladder
O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(2n)
The shape is the algorithm's cost.
The constant is what your hardware buys.
Linear vs binary search
Same target, same array: 20 probes vs 5.
That gap is O(n) vs O(logn).
The library changes the constant, not the class
- Sort at n=16000: 15.6 ms scratch vs 1.28 ms built-in (~12×)
- Same-shaped curve, gentler slope
- Win the Big-O first — no tuning rescues the wrong shape
Takeaway
Ask two questions of every algorithm:
- What's the Big-O? (this chapter)
- What's the constant? (the rest of the book)