Skip to content

Quickselect

finding

Level 3 of 3

Also called Hoare's selection algorithm.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from quickselect's own run on the numbers shown.

choose one

question 1

Quickselect looks for place 2 in 38, 61, 14, 52, 27, 45. What is the row just after its first pivot is placed?

386114522745
the answer, and why

the answer is C

The pivot is 45. 38 stays, 14 swaps with the 61, then 27 swaps with the 61. 45 swaps with the 52 just after them, to place 4.

386114522745pivot: the last number386114522745smaller: already left386114522745not smaller: stays381461522745smaller: swapped left381461522745not smaller: stays381427526145smaller: swapped left381427456152pivot placed
The first split, a row for each step, the pivot 45.

if you chose another

  • That is the whole row in order. Quickselect never sorts it.
  • That stops before the pivot swaps into place.
  • That is the row before the split.

new numbers

question 2

How many comparisons does quickselect make asking 9, 8, 7, 6, 5, 4, 3 for place 4?

9876543
the answer, and why

the answer is D

Each pivot is the smallest or the biggest left, in turn, so each part is one shorter. 6 + 5 + 4 + 3 + 2 + 1 = 21.

levelcomparisonsplace 4098765436138765495238765494334765893434765892534567891634567890found345678921
Each row rules out one number, from the bottom or the top in turn, until 6 is left alone at place 4.

if you chose another

  • That is asking for place 7, where the second pivot, 9, is found.
  • That is the first split alone. The search keeps splitting.
  • That counts the pivot as compared with itself, once in each of the six splits, 7 + 6 + 5 + 4 + 3 + 2.

new numbers

question 3

Which search costs quickselect the fewest comparisons? Each option is a row and the place asked for, as the lead writes them.

the answer, and why

the answer is D

The first pivot, 21, lands at place 3, so asked for place 3 it is the answer at once, 5 comparisons.

place asked forafter 21 lands at place 3comparisons
321 is the answer5
1keep 15, 8; 8 is found6
6keep 27, 33, 40; 40 is found7
4keep 27, 33, 40; two more splits8
The four options in order, a to d.

if you chose another

  • Place 1 is left of 21, so one more split.
  • Place 6 is right of 21, so one more split.
  • Place 4 is right of 21, and takes two more splits.

new numbers