← chapter

Quicksort

Partition around a pivot: smaller left, larger right. Recurse into each side. In place, no merge.

Partition is the operation

Watch the partition

Purple pivot · yellow compares · blue = smaller · green = placed.

The pivot is everything

Last-element pivot on sorted input = O(n²). 141× slower at n=4000.

Tame the worst case

Takeaway

Fastest in-place sort — if you guard the pivot. Avg O(n log n), worst O(n²). Next: heapsort — O(n log n) guaranteed AND in place.