← chapter

Quickselect

Find the k-th smallest in O(n) — without sorting. Quicksort's partition, recursing into ONE side.

One change from quicksort

Watch it narrow to rank k

Pink = target rank. Dark = discarded regions. Each partition throws away a whole side.

O(n) select vs O(n log n) sort

Quickselect ~ties C sort (Python handicap). heapq for the median: 6× worse — wrong k.

Match the tool to k

Takeaway

Compute just enough order to place one element. Median-of-medians guarantees linear worst case. Next tier: trees.