Skip to content

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.

levelcomparisonsplace 508547266913925831748147266913583174928552261331475869749285232613314758697492851found26133147586974928516
Quickselect on the starting row, asked for place 5, one split a level. The solid square is each split's pivot; struck grey squares are ruled out; the small triangle marks place 5. The first split: 6 numbers are smaller, so 74 lands at place 7; 5 is left of 7, so the left side is kept. Inside a side, the numbers may change order. Each split compares every other number with the pivot once: 9 numbers cost 8, 6 cost 5, 3 cost 2, 2 cost 1. 8 + 5 + 2 + 1 = 16 comparisons, and the ringed number, 58, is the answer.

or make one

race these, up to four

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.

quick sort then pick

done in 18 comparisons
132631475869748592

merge sort then pick

done in 24 comparisons
132631475869748592

quickselect

done in 16 comparisons
261331475869749285

what 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

The same racers on bigger shuffled rows: average comparisons up, how many numbers across, each labelled grid line ten times the last. A solid line is measured; a dashed one carries on the formula its own page derives.
102104106108101102103104105106107101102103104105106107108109101102103104105106107
  • 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 pickmerge sort then pickquickselect
the starting row85, 47, 26, 69, 13, 92, 58, 31, 74; 5182416
1,000 numbers10,8318,7202,939
1,000,000 numbers24,785,48218,715,9602,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.

levelcomparisonsplace 50123456789811234567897212345678963123456789541234567894found12345678930
1 to 9 in order, asked for place 5. 8 + 7 + 6 + 5 + 4 = 30 comparisons, and the ringed number, 5, is the answer.

Their comparisons on the row the race makes in each shape.

quick sort then pickmerge sort then pickquickselect
shuffled8, 3, 7, 2, 5, 9, 4, 1, 6; 5192216
in order1, 2, 3, 4, 5, 6, 7, 8, 9; 5362030
back to front9, 8, 7, 6, 5, 4, 3, 2, 1; 5361336
nearly in order1, 2, 4, 3, 5, 6, 8, 7, 9; 5282024
many repeats2, 1, 2, 3, 3, 1, 2, 3, 2; 5202118