Insertion sort works on 8, 3, 6, 1, 5. Its first slide places the 3. What is the row after its third slide?
the answer, and why
the answer is A
Slide 1 gives 3, 8, 6, 1, 5. Slide 2 stops the 6 at the 3, giving 3, 6, 8, 1, 5. Slide 3 takes the 1 to the front, giving 1, 3, 6, 8, 5.
if you chose another
BThat is the row after slide 2.
CThat is the row sorted, after slide 4.
DThat stops the 1 at the 3, but the 3 is bigger too.
2question 2
How many comparisons does insertion sort make on 2, 5, 1, 4, 3, from start to finish?
the answer, and why
the answer is A
The 5 makes 1. The 1 passes the 5 and the 2: 2, and reaching the front adds none. The 4 passes the 5 and stops at the 2: 2. The 3 passes the 5 and the 4 and stops at the 2: 3. So 1 + 2 + 2 + 3 = 8.
if you chose another
BThat counts the moves.
CThat is the most 5 numbers can cost, 1 + 2 + 3 + 4, on 5, 4, 3, 2, 1.
DThat is the fewest, one for each number placed, on 1, 2, 3, 4, 5.
3question 3
A shuffle costs insertion sort about half the moves of a row back to front. Which statement about its moves on average is both true and tight?
the answer, and why
the answer is B
On average , between an eighth and a quarter of from 2 numbers on. Half of a cost between two fixed multiples of is still between two fixed multiples of .
CTrue, since the moves are at least , at least a quarter of from 2 numbers on. Not tight: the moves' own floor, , grows like , faster than .
DFrom 2 numbers on, the moves are at least , an eighth of times . That passes 100 times once an eighth of passes 100. At 800 numbers both are 80,000, so it passes from 801 numbers on, and any fixed multiple of likewise.
4question 4
Each of these rows of 1 to 5 makes insertion sort move numbers exactly 4 times. On which does it make the most comparisons?
the answer, and why
the answer is C
Each number placed makes one comparison per move, plus one unless it reaches the front. All four rows make 4 moves, so the dearest has nothing reach the front. In 1, 3, 5, 4, 2 the 1 comes first and no slide reaches the front, so 4 moves plus 4 numbers placed, 4 + 4 = 8.
if you chose another
AThe 1 reaches the front, which saves a comparison, so this costs 7.
BThe 2 and the 1 reach the front, which saves two, so this costs 6.
DThe 1 reaches the front, which saves a comparison, so this costs 7.
pen and paper
5question 5about 15 minutes
Show that insertion sort makes moves on average, over every shuffle of different numbers.
Say what one move does, and why there is exactly one move for each pair that starts the wrong way round.
Use the average number of such pairs to finish the count.
Check the formula on the 6 shuffles of 1, 2 and 3.
a hint
Look at the two numbers a move swaps, and at which pairs change their order.
a worked answer
A move trades the number being placed with a bigger neighbour. Every other number stands left of both or right of both, so only that wrong pair changes, and is mended. The sorted row has no wrong pair, so the moves are the wrong pairs the row starts with. Each pair is wrong in half the shuffles, so the moves average half of the pairs, . For 3 numbers the moves are 0, 1, 1, 2, 2 and 3, which make 9, and 9 over 6 is 1.5.
How many moves does a shuffle take, on average?23 of 23 shown
1the move: see what one move does
the line:
why it is allowed: The bigger number steps right as the number being placed steps left. In 3, 8, 5, placing the 5 moves the 8, giving 3, 5, 8.
2the move: a move mends one inversion
the line:
pair
in 4, 9, 7, 2
in 4, 7, 9, 2
4 and 9
right
right
4 and 7
right
right
4 and 2
wrong
wrong
9 and 7
wrong
right
9 and 2
wrong
wrong
7 and 2
wrong
wrong
Every pair, before and after the move of the 9 and the 7. Only that pair changes.
why it is allowed: The two stand the wrong way round, and the move turns them. Every other number stands left of both or right of both, before and after, so no other pair changes.
guess it, then
3the move: count the moves as inversions
the line:
why it is allowed: By line 2, and the sorted row has none left. 4, 9, 7, 2 has 4 inversions and takes 4 moves.
guess it, then
4the move: count the pairs
the line:
why it is allowed: A pair is any two numbers. Count each at its second number, place 2 pairing with 1 number, place 3 with 2, up to place with . For 4 numbers, 1 + 2 + 3.
guess it, then
5the move: each pair is wrong half the time
the line:
pair 1 and 2 wrong
traded, now right
2, 1, 3
1, 2, 3
2, 3, 1
1, 3, 2
3, 2, 1
3, 1, 2
The 1 and the 2 traded. 3 of the 6 shuffles have that pair wrong, and 3 right.
why it is allowed: The numbers are all different. Trading a pair's two numbers turns a shuffle with it wrong into one with it right, and back.
guess it, then
6the move: count the wrong pairs two ways
the line:
shuffle
1 and 2
1 and 3
2 and 3
wrong pairs
1, 2, 3
0
1, 3, 2
x
1
2, 1, 3
x
1
2, 3, 1
x
x
2
3, 1, 2
x
x
2
3, 2, 1
x
x
x
3
in all
3
3
3
9
The 6 shuffles of 1, 2 and 3, an x where a pair stands wrong.
why it is allowed: Mark an x where a pair is wrong. Adding along the rows or down the columns counts each mark once, so both give 9.
guess it, then
7the move: add up the wrong pairs pair by pair
the line:
why it is allowed: Each column counts the shuffles with its pair wrong, half of them by line 5. For 3 numbers, 3 pairs times 3 is 9.
guess it, then
8the move: divide by the shuffles
the line:
why it is allowed: Line 7's total over the number of shuffles. For 3 numbers, 9 over 6 is 1.5.
guess it, then
9the move: the average moves are the average wrong pairs
the line:
why it is allowed: By line 3 each shuffle's moves are its wrong pairs, so their averages match, line 8's.
guess it, then
10the move: put in the count of pairs
the line:
why it is allowed: is the moves averaged over every shuffle of different numbers. By line 9, half the pairs of line 4.
guess it, then
11the move: use the page on adding up, reading its n as 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: As in the first ladder's line 7. For 3 numbers, 2 times 3, halved, is 3.
guess it, then
12the move: tidy the bracket
the line:
why it is allowed: One more than is .
guess it, then
13the move: multiply the fractions
the line:
why it is allowed: Half of a half is a quarter. For 3 numbers, 2 times 3 over 4 is 1.5, as in line 8.
guess it, then
14the move: write the product the other way round
the line:
why it is allowed: The same product.
guess it, then
15the move: multiply out the bracket
the line:
why it is allowed: lots of make , and lots of 1 make .
guess it, then
16the move: take a quarter of each part
the line:
why it is allowed: A quarter of a difference is the difference of the quarters.
guess it, then
17the move: a ceiling
the line:
why it is allowed: Line 16 takes something away from .
guess it, then
18the move: read it as a bound
the line:
why it is allowed: By line 17.
guess it, then
19the move: a fact for the floor
the line:
2
0.5
0.5
4
1
2
10
2.5
12.5
100
25
1,250
Equal at 2 numbers, then the eighth of n squared pulls ever further ahead.
why it is allowed: Times 8, this says twice is at most times , as at the first ladder's line 14.
guess it, then
20the move: a floor
the line:
why it is allowed: Line 16 takes away , at most line 19's , and taking away more leaves less. For 10 numbers, 22.5 is at least 25 less 12.5.
guess it, then
21the move: tidy the floor
the line:
why it is allowed: A quarter less an eighth is an eighth.
guess it, then
22the move: read it as a bound
the line:
the average moves,
the ceiling of line 17,
the floor of line 21,
Log-log, from 10 to ten million numbers. The average moves run between the ceiling and the floor, which stay the same distance apart, the ceiling always twice the floor.
why it is allowed: By line 21, from 2 numbers on.
guess it, then
23the move: a ceiling and a floor together
the line:
why it is allowed: Lines 22 and 18 bound by the same growth, .
guess it, then
try it on small rows: over every order of 3 numbers, the average is
a move mends exactly one wrong pair and makes none
the sorted row has no wrong pair, so the moves are the wrong pairs the row starts with
each pair is wrong in half the shuffles, so the average is half of
9 moves over the 6 shuffles of 3 numbers, 1.5
6question 6about 12 minutes
A list of different numbers, at least 3, was in order yesterday, such as 1, 2, 3, 4. Today its last two numbers have traded places, as in 1, 2, 4, 3, and it must be sorted again.
How many comparisons does insertion sort make on today's list?
How many moves does it make on average on a shuffled list? Use question 5.
For 1,000 numbers, compare today's moves with a shuffle's. Is insertion sort a good choice for today's list?
a hint
Ask which numbers have to slide, and how far.
a worked answer
Each number placed before the last stops at its first comparison, in all. The last passes the biggest and stops at the next, 2 comparisons and 1 move. That is comparisons, 1,000 for 1,000 numbers, and 1 move. A shuffled list makes moves on average, 249,750 for 1,000, against today's 1. So insertion sort suits today's list well.
What does today's list cost?5 of 5 shown
1the move: place the numbers before the last
the line:
why it is allowed: Places 1 to hold yesterday's first numbers in order, and place the biggest. Each number at places 2 to finds a smaller left neighbour and stops, numbers.
2the move: place the last number
the line:
why it is allowed: It is the second biggest. The biggest, on its left, steps right, one comparison and one move. The next number is smaller and stops it, and there is one, since the list holds 3 or more.
guess it, then
3the move: add them
the line:
why it is allowed: is today's comparisons, by lines 1 and 2. On 1, 2, 4, 3 the 2 and the 4 make one each, and the 3 makes 2.
guess it, then
4the move: open the bracket
the line:
why it is allowed: Adding is adding and taking away 2.
guess it, then
5the move: the 2s cancel
the line:
why it is allowed: Taking away 2 and adding 2 leaves . For 4 numbers, 4, as line 3 counts.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand