Selection sort works on 5, 9, 3, 8, 1, 6. Its first pass swaps the 1 into place 1. What is the row after its third pass?
the answer, and why
the answer is B
Pass 1 swaps the 1 and the 5, giving 1, 9, 3, 8, 5, 6. Pass 2 swaps the 3 and the 9, giving 1, 3, 9, 8, 5, 6. Pass 3 swaps the 5 and the 9, giving 1, 3, 5, 8, 9, 6.
if you chose another
AThat is the row after pass 2.
CThat is the row sorted, two passes later.
DThat shifts the 9 and the 8 along to make room for the 5, as insertion sort would. Selection sort swaps the 5 with the 9.
2question 2
How many comparisons does selection sort make on 6, 2, 7, 1, 5, 3, 4, from start to finish?
the answer, and why
the answer is B
The six passes compare the 6, 5, 4, 3, 2 and 1 numbers after their places, so 6 + 5 + 4 + 3 + 2 + 1 = 21.
if you chose another
AThat counts the swaps.
CThat is one comparison per pass, but each pass compares every number after its place.
DThat is insertion sort's count on this row.
3question 3
On a row back to front, selection sort makes few swaps, 5 on 10 numbers. Which statement about its comparisons on such a row is both true and tight?
the answer, and why
the answer is A
Every pass looks at every number left, so a row back to front costs , as every row does. That is , by the learn page's first ladder.
BIts swaps stay under , but every pass still looks at every number left.
CNo fixed multiple of stays above it. is times half of , and half of passes any fixed number. 10 times is passed from 22 numbers on, 231 against 220.
DTrue but not tight. is times half of , and half of is at least 1 from 3 numbers on. The tight bound needs a floor of .
4question 4
Selection sort works on each of these rows of 1 to 5. On which does it make the most swaps?
the answer, and why
the answer is C
A pass swaps unless its smallest is already home. On 3, 1, 5, 2, 4 every pass swaps. The row becomes 1, 3, 5, 2, 4, then 1, 2, 5, 3, 4, then 1, 2, 3, 5, 4, then 1, 2, 3, 4, 5. That is 4 swaps, the most 5 numbers can need.
if you chose another
APasses 1 and 2 swap the 1 with the 5 and the 2 with the 4, and then all is home, so 2 swaps.
BOnly the passes for places 1 and 3 swap, so 2 swaps.
DOnly the pass for place 4 swaps, so 1 swap.
pen and paper
5question 5about 15 minutes
Every row of numbers costs selection sort comparisons. Show it, and show that this count is .
Count one pass's comparisons, and say why the numbers do not change that.
Add up the passes and reach a formula.
Find a ceiling and a floor with the same term, and name the tight bound.
a hint
Count the numbers each pass looks at, then add 1 + 2 + ... up to .
a worked answer
The pass for place compares each of the numbers after it once, whatever they are. The passes make , which is . That is : at most , and at least from 2 numbers on, so . For 4 numbers, 3 + 2 + 1 makes 6.
Why does every row of numbers take comparisons?17 of 17 shown
1the move: count one pass
the line:
why it is allowed: Each of the numbers after place is compared once. On 5, 8, 3, 9, 4, pass 2 compares 3 numbers, 5 less 2. Their order never changes how many.
2the move: count the passes
the line:
why it is allowed: Once places 1 to hold the smallest numbers, the one left is the biggest, already at place .
guess it, then
3the move: add up the passes
the line:
why it is allowed: is the comparisons on any row of numbers, line 1 added over line 2's passes. For 4 numbers, 3 + 2 + 1.
guess it, then
4the move: add them the other way round
the line:
why it is allowed: The same numbers in the other order. For 4 numbers, 1 + 2 + 3 is 3 + 2 + 1, 6.
guess it, then
5the move: use the page on adding up, for a sum that stops at n - 1
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: Line 4's sum ends at . The box above shows 1 + 2 + ... + 5 is 5 times 6, halved, and the same for any last number. For 4 numbers, 3 times 4, halved, is 6.
guess it, then
6the move: tidy the bracket
the line:
why it is allowed: One more than is .
guess it, then
7the move: write the product the other way round
the line:
why it is allowed: The same product.
guess it, then
8the move: multiply out the bracket
the line:
why it is allowed: lots of make , and lots of 1 make . For 4 numbers, 4 times 3 is 12, which is 16 less 4.
guess it, then
9the move: halve each part
the line:
why it is allowed: Half of a difference is the difference of the halves.
guess it, then
10the move: a ceiling
the line:
why it is allowed: Line 9 takes something away from . For 4 numbers, 6 is at most 8, half of 16.
guess it, then
11the move: read it as a bound
the line:
why it is allowed: By line 10, stays under a fixed multiple of , a half.
guess it, then
12the move: a fact about n
the line:
why it is allowed: Now a floor. is times half of , and half of is at least 1 from 2 numbers on. For 10 numbers, 10 is at most 50.
guess it, then
13the move: a fact for the floor, halving both sides
the line:
2
1
1
4
2
4
10
5
25
100
50
2,500
Equal at 2 numbers, then the quarter of n squared pulls ever further ahead.
why it is allowed: Line 12 with both sides halved. For 10 numbers, 5 is at most 25, so line 14 can take away 25 instead of line 9's 5, a part made of alone.
guess it, then
14the move: a floor
the line:
why it is allowed: For 10 numbers, 45 is at least 50 less 25. Line 9 takes away ; line 13's is at least that, and taking away more leaves less.
guess it, then
15the move: tidy the floor
the line:
why it is allowed: Half less a quarter is a quarter.
guess it, then
16the move: read it as a bound
the line:
floor,
ceiling,
2
1
1
2
4
4
6
8
10
25
45
50
100
2,500
4,950
5,000
From 2 numbers on, sits between a quarter and a half of . That squeeze is .
why it is allowed: By line 15, stays at or above a fixed multiple of , a quarter, from 2 numbers on. A floor need only hold for rows long enough; at 1 number is 0.
guess it, then
17the move: a ceiling and a floor together
the line:
why it is allowed: Lines 16 and 11 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the pass for place k makes n - k comparisons on every row
n - 1 passes, so the sum (n - 1) + (n - 2) + ... + 1
the sum reached as n(n - 1)/2, with a reason
a ceiling and a floor of n squared, and the tight bound named
6question 6about 15 minutes
On a slow store, writing costs far more than comparing: swapping two records takes as long as 100 comparisons. A shuffled list of different records is sorted with selection sort. For 1,000 records, take the harmonic sum as about 7.49.
How many swaps does it make, on average? Show your working.
Counting each swap as 100 comparisons, what does the whole sort cost on average for 1,000 records?
Which part of that cost grows faster as the list grows, and what does that say about choosing selection sort?
a hint
A pass swaps unless its smallest is already home; ask how likely that is when the numbers left are a shuffle.
a worked answer
The pass for place swaps unless the smallest of the numbers left is already home. Those numbers are still a shuffle, so that happens with chance . A tally per pass averages to its chance of a swap, and the averages add up. So the swaps average , about 1,000 less 7.49, which is 992.51, for 1,000 records. The comparisons are 499,500 on every list of 1,000, by question 5. The swaps cost about 100 times 992.51, which is 99,251, so the whole cost is about 499,500 plus 99,251, about 598,751. The swaps stay under while the comparisons grow like , so the comparisons take over on long lists. Selection sort's few swaps pay only when each costs a lot and the list is short.
How many swaps does a shuffled row of different numbers take, on average?14 of 14 shown
1the move: when a pass swaps
the line:
why it is allowed: By line 1 of the learn page's ladder on the most swaps, a pass swaps its smallest in unless it is already home. The numbers not yet placed stand at places to , which is places. Places 2 to 5 are 4 places, 5 less 2, plus 1.
2the move: what is left is still a shuffle
the line:
shuffle
the 1 at place
after pass 1
places 2 and 3
1, 2, 3
1
1, 2, 3
2, 3
2, 1, 3
2
1, 2, 3
2, 3
3, 2, 1
3
1, 2, 3
2, 3
1, 3, 2
1
1, 3, 2
3, 2
3, 1, 2
2
1, 3, 2
3, 2
2, 3, 1
3
1, 3, 2
3, 2
Pass 1 on each shuffle of 1, 2 and 3, grouped by what it leaves. Each order of the 2 and the 3 comes from three shuffles, one for each place the 1 started at.
why it is allowed: Undo pass 1's swap and the 1 goes back where it started. So each order of the rest comes from one shuffle per place the 1 could start at, in all. In the picture, 3, 2 comes from 1, 3, 2, from 3, 1, 2 and from 2, 3, 1. So the next pass meets a fresh shuffle, one number shorter, and the same holds for it.
guess it, then
3the move: the chance of no swap
the line:
Each arrow trades the two places underlined in red, pairing up the shuffles as they stand before sorting, and trading back undoes it. Each place holds the 1 in 2 of the 6 shuffles.
why it is allowed: By lines 1 and 2 the numbers left are a shuffle. In 2, 3, 1, trading places 1 and 3 gives 1, 3, 2, and trading back undoes it. So each order with their smallest at one place pairs with one with it at the other. So it stands at each place equally often, and at place , the first, in 1 of every orders. In the picture the 1 is at each place in 2 of the 6 shuffles, a third.
guess it, then
4the move: the swaps are a sum of tallies
the line:
why it is allowed: Each pass gets a tally, 1 when it swaps and 0 when not. Adding a row's tallies counts its swaps.
guess it, then
5the move: average tally by tally
the line:
shuffle
pass 1
pass 2
swaps
1, 2, 3
0
0
0
1, 3, 2
0
1
1
2, 1, 3
1
0
1
2, 3, 1
1
1
2
3, 1, 2
1
1
2
3, 2, 1
1
0
1
in all
4
3
7
average
4 over 6
3 over 6
7 over 6
Each pass's tally on every shuffle of 1, 2 and 3, and each row's swaps.
why it is allowed: Total the swaps column, or total each pass's column and add. Each tally is counted once either way, and dividing by the shuffles keeps them equal. In the picture, 4 and 3 make 7, and 4 over 6 and 3 over 6 make 7 over 6.
guess it, then
6the move: a tally's average is its chance
the line:
why it is allowed: A column of 1s and 0s averages to its share of 1s. In line 5's picture pass 1 swaps in 4 of the 6 shuffles, two thirds, and pass 2 in 3, a half.
guess it, then
7the move: a swap or none
the line:
why it is allowed: A pass either swaps or not, so the two chances add to 1, and line 3 gives the chance of none. Two thirds is 1 less a third, and a half is 1 less a half.
guess it, then
8the move: add up the passes
the line:
why it is allowed: is the swaps averaged over every shuffle of different numbers. By lines 5 to 7, place 1 adds , down to for place . For 3 numbers, two thirds plus a half, 7 over 6.
guess it, then
9the move: take the ones out
the line:
why it is allowed: brackets, each 1 less a fraction. The ones make , and every fraction is taken away.
guess it, then
10the move: add the fractions the other way round
the line:
why it is allowed: The same fractions in the other order.
guess it, then
11the move: name the sum
the line:
why it is allowed: 1, plus a half, and on up to 1 over , is the harmonic sum, . This sum lacks its first 1.
guess it, then
12the move: open the bracket
the line:
why it is allowed: Taking away less 1 is taking away and giving the 1 back.
guess it, then
13the move: put the 1 next to the n - 1
the line:
why it is allowed: Adding and taking away give the same in either order.
guess it, then
14the move: collect the ones
the line:
why it is allowed: is . For 3 numbers, 3 less about 1.83 is about 1.17, line 8's 7 over 6. is more than 1 from 2 numbers on, so the average stays under , the most swaps by the learn page's ladder on them.
guess it, then
try it on small rows: over every order of 3 numbers, the average is about 1.17