Skip to content

Quick sort

ordering

Level 2 of 3

Also called Partition-exchange sort.

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 quick sort's own run on the numbers shown.

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.

new numbers

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.

new numbers

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.

new numbers