Skip to content

Randomised quick sort

ordering

Level 3 of 3

Also called Randomised quicksort.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

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

choose one

question 1

Randomised quick sort works on 19, 42, 33, 11, 25, 48, and its first pick is the 33. What is the row just after the 33 is placed?

194233112548
the answer, and why

the answer is D

The 33 swaps to the end: 19, 42, 48, 11, 25, 33. The 19 is smaller and stays. The 11 swaps with the 42: 19, 11, 48, 42, 25, 33. The 25 swaps with the 48: 19, 11, 25, 42, 48, 33. Then the 33 swaps with the 42 just after them.

if you chose another

  • That sorts both sides too, which waits for later splits.
  • That stops before the pivot swaps into place.
  • That is the row just after the pivot moves to the end.

question 2

How many comparisons does randomised quick sort make on 6, 6, 6, 6, 6, 6, whatever its picks?

666666
the answer, and why

the answer is D

No number is smaller than its pivot, so the parts hold 6, 5, 4, 3, 2 numbers. That is 5 + 4 + 3 + 2 + 1 = 15.

if you chose another

  • That is the first split alone.
  • That counts each part of m numbers as m comparisons, the pivot with itself too, down to the last lone number.
  • That is one comparison for each number.

question 3

Four different numbers. What is the chance that every pick is the biggest or smallest left?

the answer, and why

the answer is C

The chances are 2 in 4, then 2 in 3, then 2 in 2. Multiplied, 8 over 24, which is 1 in 3.

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

if you chose another

  • That counts one unlucky place of 4 at the first split, and stops there.
  • That takes one unlucky place of 4, then one of 3, and leaves out the last split.
  • That takes one unlucky place each time instead of two, 1 in 4, then 1 in 3, then 1 in 2.

question 4

Which is true of randomised quick sort?

the answer, and why

the answer is C

The average over the picks is 2(n+1)H(n)−4n2(n + 1)H(n) - 4n on every row of different numbers, whatever its order.

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

if you chose another

  • Unlucky picks still cost that much.
  • Equal numbers cost n(n−1)2\frac{n(n - 1)}{2}, whatever the picks.
  • The picks change only the work, never the sorted row.

pen and paper

question 5about 10 minutes

Randomised quick sort works on 7, 7, 7, 7, 7.

  1. Write each level of parts 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 the picks make no difference here.
a hint

Ask which numbers a split moves to the small side.

a worked answer

No number is smaller than its pivot, so each split places its pivot and leaves the rest in one part. The parts hold 5, 4, 3 and 2 numbers, then 1, making 4, 3, 2 and 1 comparisons: 10 in all, and 5 times 4 over 2 is 10. Whatever is picked, every pivot is equal to the rest.

Why does a row of equal numbers cost n(n−1)2\frac{n(n - 1)}{2} comparisons, whatever the picks?
  1. the move: no number is smaller

    the line:
    with every number equal,none is smaller than the pivot\begin{gathered} \text{with every number equal,} \\ \text{none is smaller than the pivot} \end{gathered}
    levelcomparisons0999994199999329999923999991499999010
    Five equal numbers. Each split places its pivot and leaves the rest in one part. 4 + 3 + 2 + 1 = 10 comparisons.

    why it is allowed: A split moves only numbers smaller than the pivot. Whatever place is picked, each split leaves all the others in one part, as the picture shows.

  2. 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.

  3. the move: add up the splits

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

    why it is allowed: EE is the comparisons on a row of nn equal numbers. By line 1 the parts hold nn, then n−1n - 1, and on down to 2 numbers. By line 2 each costs one less.

  4. the move: add the counting numbers

    the line:
    E=(n−1)((n−1)+1)2E = \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:
    E=n(n−1)2E = \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: a ceiling

    the line:
    E≤n22E \le \frac{n^2}{2}

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

  7. the move: read it as a bound

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

    why it is allowed: By line 6, EE stays under half of n2n^2.

  8. the move: a floor

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

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

  9. the move: read it as a bound

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

    why it is allowed: By line 8, from 2 numbers on, EE never drops below a quarter of n2n^2.

  10. the move: ceiling and floor together

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

    why it is allowed: Lines 7 and 9 bound EE by the same growth, n2n^2.

try it with a small nn: at n=5n = 5, line 3 gives E=10E = 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 levels, each part one shorter.
  • The total 10 matching the formula.
  • No number is smaller, whatever the pick.

question 6about 10 minutes

Randomised quick sort works on 1, 2, 3, each first pick as likely.

  1. List the three first picks and the comparisons each leads to.
  2. Average them.
  3. Check your answer against 2(n+1)H(n)−4n2(n + 1)H(n) - 4n.
a hint

Every run compares the first pivot with the other two.

a worked answer

A first pick of 1 or 3 leaves a part of two numbers, one more comparison: 3 in all. A first pick of 2 leaves two parts of one number: 2 in all. The average is 3, plus 2, plus 3, over 3: 8 over 3. For the formula, H(3)H(3) is 1, plus a half, plus a third: 11 over 6. So it gives 8 times 11 over 6, less 12: also 8 over 3.

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

what a full answer includes
  • The three picks and their 3, 2 and 3 comparisons.
  • The average, 8 over 3.
  • The formula giving the same.