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?
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
AThat sorts both sides too, which waits for later splits.
BThat stops before the pivot swaps into place.
CThat is the row just after the pivot moves to the end.
2question 2
How many comparisons does randomised quick sort make on 6, 6, 6, 6, 6, 6, whatever its picks?
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
AThat is the first split alone.
BThat counts each part of m numbers as m comparisons, the pivot with itself too, down to the last lone number.
CThat is one comparison for each number.
3question 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.
DThe picks change only the work, never the sorted row.
pen and paper
5question 5about 10 minutes
Randomised quick sort works on 7, 7, 7, 7, 7.
Write each level of parts and count its comparisons.
Add them up, and check the total against .
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 comparisons, whatever the picks?10 of 10 shown
1the move: no number is smaller
the line:
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.
2the move: price one split
the line:
why it is allowed: Each number but the pivot is compared with the pivot, once.
guess it, then
3the move: add up the splits
the line:
why it is allowed: is the comparisons on a row of equal numbers. By line 1 the parts hold , then , and on down to 2 numbers. By line 2 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.
Randomised quick sort works on 1, 2, 3, each first pick as likely.
List the three first picks and the comparisons each leads to.
Average them.
Check your answer against .
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, 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.