← capítulo

Non-comparison sorts

Beat O(n log n) by NOT comparing — use keys as indices. Counting · radix · bucket. Linear, on bounded integers.

The loophole

Watch it count, not compare

Each element bumps its bar. No two elements ever compared. Read bars left to right = sorted.

Counting sort beats the library

Counting: 3.3× faster than Timsort on bounded keys. (Radix loses — 7 Python passes.)

The catch: the range k

Takeaway

Specialists: linear on bounded integer keys, unbeatable there. The rare case your code beats the library. Next: searching — binary search.