Skip to content

Quick sort

ordering

Level 2 of 3

Also called Partition-exchange sort.

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

Quick sort starts on 33, 8, 27, 14, 46, 21. What is the row just after its first pivot is placed?

33827144621
the answer, and why

the answer is B

The pivot is 21. The 8 swaps with the 33, onto the small side. Then the 14 swaps with the 33. Then 21 swaps with the 27 just after them.

if you chose another

  • That sorts the right part too, which waits for later splits.
  • That stops before the pivot swaps into place.
  • That is the row before the split.

question 2

How many comparisons does quick sort make on 1, 2, 3, 4, 5, 6, 7?

1234567
the answer, and why

the answer is B

Each pivot is the biggest left, so the parts hold 7, 6, 5, 4, 3, 2 numbers. That is 6 + 5 + 4 + 3 + 2 + 1 = 21.

if you chose another

  • That is the first split alone. Quick sort keeps splitting.
  • That is the cheapest row of 7.
  • That counts 7 comparisons for the first split, but the pivot is not compared with itself.

question 3

Which row costs quick sort the fewest comparisons?

the answer, and why

the answer is B

Its first pivot, 4, is the middle in size, and so are the pivots after it. That costs 13. The rows in order and back to front cost 28, and 42, 7, 31, 18, 56, 23, 12, 49 costs 17.

if you chose another

  • In order, every pivot is the biggest left, the dearest kind of row.
  • Back to front, the pivots are the smallest and the biggest left in turn, which is just as dear.
  • Its first pivot, 49, leaves almost everything on one side. It costs 17.

question 4

Which is true of quick sort's comparisons?

the answer, and why

the answer is C

A row in order costs n(n−1)2\frac{n(n - 1)}{2}, which is Θ(n2)\Theta(n^2). The average over shuffles and the cheapest row are Θ(n log⁡2 n)\Theta(n \,\log_2\, n).

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

if you chose another

  • A row in order grows like n2n^2, so not every row does.
  • The average over shuffles is Θ(n log⁡2 n)\Theta(n \,\log_2\, n).
  • The cheapest row of 8 costs 13, not 7.

pen and paper

question 5about 10 minutes

Quick sort works on a row already in order, 1, 2, 3, 4, 5, 6.

  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 no row of 6 numbers costs more.
a hint

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

a worked answer

The parts hold 6, 5, 4, 3, 2 numbers and then 1. So the levels make 5, 4, 3, 2 and 1 comparisons: 15 in all, and 6 times 5 over 2 is 15. Two numbers meet only when one is the pivot, and then never again, so no row compares a pair twice. Here every pair meets once.

Why does a row in order cost n(n−1)2\frac{n(n - 1)}{2} comparisons, the most any row 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 split

    the line:
    in order, a split leavesone part of m−1 numbers,still in order\begin{gathered} \text{in order, a split leaves} \\ \text{one part of } m - 1 \text{ numbers,} \\ \text{still in order} \end{gathered}
    levelcomparisons0123454112345321234523123451412345010
    A row in order of 5. Each part is one shorter than the last. 4 + 3 + 2 + 1 = 10 comparisons.

    why it is allowed: The pivot, the last number, is the biggest. So every other number is smaller and stays on its left, in order, and nothing goes right.

  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 on a row in order. By line 2 the parts hold nn, then n−1n - 1, and on down to 2 numbers. 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 costs more

    the line:
    each pair meets at most once,so no row costs more than W\begin{gathered} \text{each pair meets at most once,} \\ \text{so no row costs more than } W \end{gathered}

    why it is allowed: Two numbers meet only when one is the pivot, which then stays outside both parts, so they never meet again. On a row in order every pair meets: the bigger of the two is a pivot first, with the smaller still in its part. So WW counts every pair once, and no row can count more. In the picture, 5 numbers make 10 pairs.

  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.

  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 half of n2n^2.

  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 half of nn once nn is 2 or more.

  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.

  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 six levels, each part one shorter.
  • The total 15 matching the formula.
  • Each pair meets at most once.

question 6about 15 minutes

Quick sort works on a shuffle of 1, 2, 3, 4, every order as likely.

  1. What share of shuffles compare 1 with 4?
  2. Add up the shares of all six pairs.
  3. Check your total against 2(n+1)H(n)−4n2(n + 1)H(n) - 4n.
a hint

Two numbers, the smaller ii and the bigger jj, meet in 2 of every j−i+1j - i + 1 shuffles. For 1 and 4, that is 2 of every 4.

a worked answer

1 and 4 meet in 2 of every 4 shuffles, a half. The three pairs 1 apart always meet: 3 in all. The two pairs 2 apart meet in 2 of every 3: 4 thirds together. In all 3, plus 4 thirds, plus a half, about 4.83. For the formula, H(4)H(4) is 1, plus a half, plus a third, plus a quarter: 25 over 12. So it gives 10 times 25 over 12, less 16: also about 4.83.

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

what a full answer includes
  • The share for 1 and 4, a half.
  • The total of the six shares, about 4.83.
  • The formula giving the same.