← capítulo

Heapsort

O(n log n) guaranteed (like merge) AND in place (like quicksort). Built on the binary heap.

The array is the tree

Watch the sorted tail grow

Orange max → swaps to the right (green sorted), new max sifts up. In place, no second array.

No bad case

Random, sorted, reversed — all the same time. (Quicksort blew up 141×.)

Best Big-O, not fastest

Takeaway

Heapsort trades everyday speed for an unconditional guarantee. Big-O and reality part ways. Next: beat O(n log n) by not comparing.