Finding the median return in a dataset, or the 95th-percentile latency in a set of measurements, is the selection problem: find the -th smallest element of unordered elements. The obvious approach — sort everything, then read off index — costs , but selection is a strictly easier problem than full sorting, and this note shows it can be solved in expected linear time by reusing quicksort's
partition subroutine in a new way: quickselect. It closes with a pointer to a worst-case-linear algorithm (median-of-medians) for when the expected-time guarantee is not enough.In this lesson
- Learning objectives
- The selection problem
- Quickselect
- Worst-case-linear selection: median-of-medians
Before you start
The full lesson, with worked examples and exercise solutions, is for members.
Read the full lessonNew to SelfMadeQuant.com? See membership options or explore the curriculum.