MEPX
Chapter 12 of 12All chapters

Chapter 12 of 12

Choosing an algorithm

When the notation is not the answer.

Small n changes everything

A tight O(n squared) beats a loose O(n log n) on small inputs, which is why real sort implementations switch to insertion sort under a few dozen elements.

  • Ask what n actually is in production before optimising the shape.
  • Cache behaviour and allocation often dominate at small sizes.

Simplicity counts

The fastest correct code nobody understands is a liability. Reach for the clearer algorithm until measurement says otherwise, then leave a comment explaining why the clever one is there.