Six questions, four to pick from and two for paper. A search is written as the row, a semicolon, then the place asked for, so 42, 7, 31; 2 asks 42, 7, 31 for place 2. Each answer stays hidden until you open it.
AAsking a row in order for its smallest costs , which is .
CIts worst, , is quick sort's worst, far more than merge sort makes.
DOn 1,000 numbers in order, merge sort makes 5,052 comparisons, and quickselect asked for the smallest 499,500.
pen and paper
5question 5about 10 minutes
Quickselect asks a row already in order, 1, 2, 3, 4, 5, 6, for place 1.
Write each part searched and count its comparisons.
Add them up, and check the total against .
Explain why no row or place of 6 numbers costs more.
a hint
The pivot is always the biggest number left in the part.
a worked answer
The parts hold 6, 5, 4, 3 and 2 numbers, then 1 alone, the answer. The splits make 5, 4, 3, 2 and 1 comparisons: 15, and 6 times 5 over 2 is 15. A pivot is then found or ruled out, so no pair meets twice. 6 numbers make 15 pairs, 5 + 4 + 3 + 2 + 1, so no search of 6 costs more. Here every pair meets once.
1 to 6 in order, asking for place 1. 5 + 4 + 3 + 2 + 1 = 15 comparisons.
Why does asking a row in order for its smallest cost comparisons, the most any search 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 asked for its smallest
the line:
1 to 5 in order, asking for place 1. 4 + 3 + 2 + 1 = 10 comparisons, and the 1 left alone is the answer.
why it is allowed: The pivot, the last number, is the biggest left. The rest are smaller and stay on its left, in order, and place 1 is there.
guess it, then
3the move: add up the splits
the line:
why it is allowed: is the comparisons asking 1 to in order for the smallest. By line 2 the parts hold , then , down to 2. 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 or place costs more
the line:
Row by row, 2, 3, 4 and 5 each pair with every smaller number, 1, 2, 3 and 4 squares. 5 numbers make 4 + 3 + 2 + 1 = 10 pairs, the same sum as .
why it is allowed: Two numbers meet only when one is the pivot. That pivot is then found or ruled out, so it is in no later part, and the pair never meets again. As the square shows, numbers make 1, plus 2, and on up to pairs, line 3's sum backwards. So no search makes more than comparisons. Here every pair meets: the bigger is a pivot first, with the smaller still in its part.
guess it, then
7the move: a ceiling
the line:
why it is allowed: In line 5, is less than . For 5, 10 is under 12.5.
guess it, then
8the move: read it as a bound
the line:
why it is allowed: By line 7, stays under a half of , and a half is the fixed multiple.
guess it, then
9the move: a floor
the line:
why it is allowed: In line 5, is at least a half of once is 2 or more. For 5, 10 is at least 6.25.
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 , and a quarter is the fixed multiple.
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
Each pair meets at most once, and 6 numbers make 15 pairs.
6question 6about 15 minutes
Quickselect asks a shuffle of 1, 2, 3, 4 for place 1, every order as likely.
What share of shuffles compare 3 with 4?
Add up the shares of all six pairs.
Quick sort makes 29 over 6 on the same shuffles. Which pairs meet less here, and why?
a hint
Asking for place 1, a pair's stretch runs from 1 to its bigger number, numbers, and it meets in 2 of every shuffles. In quick sort the stretch is just to , as on quick sort's page.
a worked answer
3 and 4 have the stretch 1 to 4, so they meet in a half of the shuffles. 1 and 2 always meet, the two pairs with 3 in 2 thirds each, the three with 4 in a half each. So 1 + 2 times 2 thirds + 3 times a half is 23 sixths, about 3.83. 2 and 3, 3 and 4, and 2 and 4 meet less than in quick sort. A pivot between the pair and place 1, or the 1 itself, found first, rules them out unmet.
pair
stretch
share of shuffles they meet in
in quick sort
1 and 2
1 to 2
1
1
1 and 3
1 to 3
2 thirds
2 thirds
2 and 3
1 to 3
2 thirds
1
1 and 4
1 to 4
a half
a half
2 and 4
1 to 4
a half
2 thirds
3 and 4
1 to 4
a half
1
in all
23 sixths
29 sixths
Every pair of 1, 2, 3, 4, asking for place 1, beside quick sort sorting the same shuffles.