Skip to content

Quickselect

finding

Level 3 of 3

Also called Hoare's selection algorithm.

test yourself

Six questions, four to pick from and two for paper. A search is written as the row, a semicolon, then the place asked for, so 42, 7, 31; 2 asks 42, 7, 31 for place 2. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

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.

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.

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.

question 4

Which is true of quickselect's comparisons?

the answer, and why

the answer is B

The average over every order and every place stays under 3n3n, and is at least n−1n - 1, so it is Θ(n)\Theta(n).

this tests best, worst and average, on the learn page

if you chose another

  • Asking a row in order for its smallest costs n(n−1)2\frac{n(n - 1)}{2}, which is Θ(n2)\Theta(n^2).
  • Its worst, n(n−1)2\frac{n(n - 1)}{2}, is quick sort's worst, far more than merge sort makes.
  • On 1,000 numbers in order, merge sort makes 5,052 comparisons, and quickselect asked for the smallest 499,500.

pen and paper

question 5about 10 minutes

Quickselect asks a row already in order, 1, 2, 3, 4, 5, 6, for place 1.

  1. Write each part searched and count its comparisons.
  2. Add them up, and check the total against n(n−1)2\frac{n(n - 1)}{2}.
  3. Explain why no row or place of 6 numbers costs more.
a hint

The pivot is always the biggest number left in the part.

a worked answer

The parts hold 6, 5, 4, 3 and 2 numbers, then 1 alone, the answer. The splits make 5, 4, 3, 2 and 1 comparisons: 15, and 6 times 5 over 2 is 15. A pivot is then found or ruled out, so no pair meets twice. 6 numbers make 15 pairs, 5 + 4 + 3 + 2 + 1, so no search of 6 costs more. Here every pair meets once.

levelcomparisonsplace 1012345651123456421234563312345624123456151234560found12345615
1 to 6 in order, asking for place 1. 5 + 4 + 3 + 2 + 1 = 15 comparisons.
Why does asking a row in order for its smallest cost n(n−1)2\frac{n(n - 1)}{2} comparisons, the most any search can?
  1. the move: price one split

    the line:
    a split of m numbersmakes m−1 comparisons\begin{gathered} \text{a split of } m \text{ numbers} \\ \text{makes } m - 1 \text{ comparisons} \end{gathered}

    why it is allowed: Each number but the pivot is compared with the pivot, once.

  2. the move: see a row in order asked for its smallest

    the line:
    each split keeps a partone shorter, still in order\begin{gathered} \text{each split keeps a part} \\ \text{one shorter, still in order} \end{gathered}
    levelcomparisonsplace 101234541123453212345231234514123450found1234510
    1 to 5 in order, asking for place 1. 4 + 3 + 2 + 1 = 10 comparisons, and the 1 left alone is the answer.

    why it is allowed: The pivot, the last number, is the biggest left. The rest are smaller and stay on its left, in order, and place 1 is there.

  3. the move: add up the splits

    the line:
    W=(n−1)+(n−2)+⋯+1W = (n - 1) + (n - 2) + \dots + 1

    why it is allowed: WW is the comparisons asking 1 to nn in order for the smallest. By line 2 the parts hold nn, then n−1n - 1, down to 2. By line 1 each costs one less.

  4. the move: add the counting numbers

    the line:
    W=(n−1)((n−1)+1)2W = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The fold shows how to add the whole numbers from 1 up to any last number, with two staircases. Here the last number is n−1n - 1.

  5. the move: tidy

    the line:
    W=n(n−1)2W = \frac{n(n - 1)}{2}

    why it is allowed: (n−1)+1(n - 1) + 1 is nn. For 5 numbers, 5 times 4 over 2 is 10, as in the picture.

  6. the move: no row or place costs more

    the line:
    each pair meets at most once,so no searchcosts more than W\begin{gathered} \text{each pair meets at most once,} \\ \text{so no search} \\ \text{costs more than } W \end{gathered}
    Row by row, 2, 3, 4 and 5 each pair with every smaller number, 1, 2, 3 and 4 squares. 5 numbers make 4 + 3 + 2 + 1 = 10 pairs, the same sum as WW.

    why it is allowed: Two numbers meet only when one is the pivot. That pivot is then found or ruled out, so it is in no later part, and the pair never meets again. As the square shows, nn numbers make 1, plus 2, and on up to n−1n - 1 pairs, line 3's sum backwards. So no search makes more than WW comparisons. Here every pair meets: the bigger is a pivot first, with the smaller still in its part.

  7. the move: a ceiling

    the line:
    W≤n22W \le \frac{n^2}{2}

    why it is allowed: In line 5, n−1n - 1 is less than nn. For 5, 10 is under 12.5.

  8. the move: read it as a bound

    the line:
    W∈O(n2)W \in O(n^2)

    why it is allowed: By line 7, WW stays under a half of n2n^2, and a half is the fixed multiple.

  9. the move: a floor

    the line:
    W≥n24 for n≥2W \ge \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: In line 5, n−1n - 1 is at least a half of nn once nn is 2 or more. For 5, 10 is at least 6.25.

  10. the move: read it as a bound

    the line:
    W∈Ω(n2)W \in \Omega(n^2)

    why it is allowed: By line 9, WW never drops below a quarter of n2n^2, and a quarter is the fixed multiple.

  11. the move: ceiling and floor together

    the line:
    W∈Θ(n2)W \in \Theta(n^2)

    why it is allowed: Lines 8 and 10 bound WW by the same growth, n2n^2.

try it with a small nn: at n=5n = 5, line 3 gives W=10W = 10, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • The parts, each one shorter.
  • The total 15 matching the formula.
  • Each pair meets at most once, and 6 numbers make 15 pairs.

question 6about 15 minutes

Quickselect asks a shuffle of 1, 2, 3, 4 for place 1, every order as likely.

  1. What share of shuffles compare 3 with 4?
  2. Add up the shares of all six pairs.
  3. Quick sort makes 29 over 6 on the same shuffles. Which pairs meet less here, and why?
a hint

Asking for place 1, a pair's stretch runs from 1 to its bigger number, ss numbers, and it meets in 2 of every ss shuffles. In quick sort the stretch is just ii to jj, as on quick sort's page.

a worked answer

3 and 4 have the stretch 1 to 4, so they meet in a half of the shuffles. 1 and 2 always meet, the two pairs with 3 in 2 thirds each, the three with 4 in a half each. So 1 + 2 times 2 thirds + 3 times a half is 23 sixths, about 3.83. 2 and 3, 3 and 4, and 2 and 4 meet less than in quick sort. A pivot between the pair and place 1, or the 1 itself, found first, rules them out unmet.

pairstretchshare of shuffles they meet inin quick sort
1 and 21 to 211
1 and 31 to 32 thirds2 thirds
2 and 31 to 32 thirds1
1 and 41 to 4a halfa half
2 and 41 to 4a half2 thirds
3 and 41 to 4a half1
in all23 sixths29 sixths
Every pair of 1, 2, 3, 4, asking for place 1, beside quick sort sorting the same shuffles.

this tests where the formula comes from, on the learn page

what a full answer includes
  • The share for 3 and 4, a half.
  • The total of the six shares, 23 over 6.
  • A pivot between the pair and place 1, or the 1 itself, found first, rules them out unmet.