← chapter

How we measure cost

Big-O: how work grows with input size n — the measuring stick for the whole book.

Count operations, not seconds

The ladder

O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(2n)O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n)

The shape is the algorithm's cost. The constant is what your hardware buys.

The shapes, growing

Linear vs binary search

Same target, same array: 20 probes vs 5. That gap is O(n)O(n) vs O(logn)O(\log n).

The library changes the constant, not the class

Takeaway

Ask two questions of every algorithm:

  1. What's the Big-O? (this chapter)
  2. What's the constant? (the rest of the book)