find the m-th smallest
The m-th smallest is the number at place m once the row is in order. Race quickselect against sorting first and then picking, on the same row and place.
Quickselect splits the row round its last number, the pivot: smaller numbers go to its left, the rest to its right. The pivot then lands at its own place in order. If that is the place asked for, the pivot is the answer and the search stops. If the place asked for is left of it, quickselect keeps the left side, else the right, and splits again.
A sort puts the whole row in order, then reads the place. In order, the starting row reads 13, 26, 31, 47, 58, 69, 74, 85, 92, and place 5 holds 58: reading it compares nothing.
or make one
race these, up to four
Choose two to four racers.
one tick is
Without scripts the race cannot run, so each lane shows where its racer ends.
Tick 24: no lane has made more than 24 comparisons. Quick sort then pick, merge sort then pick and quickselect have finished.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
quick sort then pick
done in 18 comparisonsmerge sort then pick
done in 24 comparisonsheap sort then pick
quickselect
done in 16 comparisonswhat the marks mean
- a number being compared
- a number moving
- a number placed for good
- a pivot, the number each split compares the rest with
- a number ruled out
- a number not yet placed for good
- how much of its race a lane has run
which suits this row and place
On these 9 numbers, quickselect made the fewest comparisons: 16, against 18 for quick sort then pick and 24 for merge sort then pick.
each racer's own page
- quick sort then pick
- merge sort then pick
- heap sort then pick
- quickselect
the starting racers as the row grows
On shuffled rows of 1,000 and of a million, quickselect makes the fewest comparisons. Its cost depends on the place: on the starting row, 14, 16 and 9 at places 1, 5 and 9. So its numbers for 1,000 and a million are averages over places along the row, not the middle alone.
First, their comparisons on the starting row, run once. Then their comparisons on shuffled rows, on average. On shuffled rows, a plain number is measured: the mean of 20 rows or more. A number in italics comes from its racer's own formula: follow its link. The fewest on each line is in bold.
| quick sort then pick | merge sort then pick | quickselect | |
|---|---|---|---|
| the starting row85, 47, 26, 69, 13, 92, 58, 31, 74; 5 | 18 | 24 | 16 |
| 1,000 numbers | 10,831 | 8,720 | 2,939 |
| 1,000,000 numbers | 24,785,482 | 18,715,960 | 2,999,898 |
the starting racers on 9 numbers of each shape
Shape can change the winner. On the rows below, merge sort then pick wins in order, back to front and nearly in order. In order, every pivot is the biggest number still in the search, so each split before the last rules out that pivot alone.
Their comparisons on the row the race makes in each shape.
| quick sort then pick | merge sort then pick | quickselect | |
|---|---|---|---|
| shuffled8, 3, 7, 2, 5, 9, 4, 1, 6; 5 | 19 | 22 | 16 |
| in order1, 2, 3, 4, 5, 6, 7, 8, 9; 5 | 36 | 20 | 30 |
| back to front9, 8, 7, 6, 5, 4, 3, 2, 1; 5 | 36 | 13 | 36 |
| nearly in order1, 2, 4, 3, 5, 6, 8, 7, 9; 5 | 28 | 20 | 24 |
| many repeats2, 1, 2, 3, 3, 1, 2, 3, 2; 5 | 20 | 21 | 18 |