Suppose you have a million exam scores and you want to know the median — the one sitting exactly in the middle. The obvious approach is to sort everything and pick the middle element, but that costs time. You're sorting elements you'll never need just to find one.
Quickselect is the algorithm that says: don't bother. Pick a pivot, partition the array around it, and you immediately know whether the element you want is to the left, to the right, or right where the pivot landed. Recurse into only the relevant half — throw the other half away entirely.
The result is expected time: linear, not linearithmic. Tony Hoare described the idea in 1961 — the same insight that gave us Quicksort — and it remains one of the most elegant tricks in classical algorithms.
The key tension in selection problems is that checking whether a value is the k-th smallest requires seeing the whole array (), while full sorting overshoots by a factor of log n. Quickselect threads the needle exactly in between.
Comments
Loading comments...