Quick sort starts on 33, 8, 27, 14, 46, 21. What is the row just after its first pivot is placed?
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
AThat sorts the right part too, which waits for later splits.
CThat stops before the pivot swaps into place.
DThat is the row before the split.
2question 2
How many comparisons does quick sort make on 1, 2, 3, 4, 5, 6, 7?
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
AThat is the first split alone. Quick sort keeps splitting.
CThat is the cheapest row of 7.
DThat counts 7 comparisons for the first split, but the pivot is not compared with itself.
3question 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
AIn order, every pivot is the biggest left, the dearest kind of row.
CBack to front, the pivots are the smallest and the biggest left in turn, which is just as dear.
DIts first pivot, 49, leaves almost everything on one side. It costs 17.
4question 4
Which is true of quick sort's comparisons?
the answer, and why
the answer is C
A row in order costs , which is . The average over shuffles and the cheapest row are .
AA row in order grows like , so not every row does.
BThe average over shuffles is .
DThe cheapest row of 8 costs 13, not 7.
pen and paper
5question 5about 10 minutes
Quick sort works on a row already in order, 1, 2, 3, 4, 5, 6.
Write each level of parts and count its comparisons.
Add them up, and check the total against .
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 comparisons, the most any row can?11 of 11 shown
1the move: price one split
the line:
why it is allowed: Each number but the pivot is compared with the pivot, once.
2the move: see a row in order split
the line:
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.
guess it, then
3the move: add up the splits
the line:
why it is allowed: is the comparisons on a row in order. By line 2 the parts hold , then , and on down to 2 numbers. By line 1 each costs one less.
guess it, then
4the move: add the counting numbers
the line:
where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make?
1the move: draw the sum as a staircase
the line:
why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
2the move: take two staircases
the line:
why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
3the move: fit them into a rectangle
the line:
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 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 , and the columns fill a rectangle wide and tall.
4the move: halve both sides
the line:
why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
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 .
guess it, then
5the move: tidy
the line:
why it is allowed: is . For 5 numbers, 5 times 4 over 2 is 10, as in the picture.
guess it, then
6the move: no row costs more
the line:
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 counts every pair once, and no row can count more. In the picture, 5 numbers make 10 pairs.
guess it, then
7the move: a ceiling
the line:
why it is allowed: In line 5, is less than .
guess it, then
8the move: read it as a bound
the line:
why it is allowed: By line 7, stays under half of .
guess it, then
9the move: a floor
the line:
why it is allowed: In line 5, is at least half of once is 2 or more.
guess it, then
10the move: read it as a bound
the line:
why it is allowed: By line 9, never drops below a quarter of .
guess it, then
11the move: ceiling and floor together
the line:
why it is allowed: Lines 8 and 10 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
Quick sort works on a shuffle of 1, 2, 3, 4, every order as likely.
What share of shuffles compare 1 with 4?
Add up the shares of all six pairs.
Check your total against .
a hint
Two numbers, the smaller and the bigger , meet in 2 of every 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, 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.