CThat is insertion sort. On 9, 7, 3, 5, 2, 4 Shell sort's first move mends 7 wrong pairs.
DOn 7, 6, 5, 4, 3, 2, 1 it makes 9.
4question 4
Shell sort works on each of these orders of 1 to 7. On which does it make the most comparisons?
the answer, and why
the answer is B
Its gap 4 pass makes 3 comparisons and leaves 12 of its 14 wrong pairs. So the gap 1 pass makes 12 moves, and 16 comparisons, one per move and one for each of 4 numbers stopping short of the front. 19 in all.
6, 4, 1, 7, 5, 3, 2 pass by pass, 3 comparisons and then 16.
if you chose another
A13 comparisons. The gap 4 pass leaves 6 of its 21 wrong pairs.
C11 comparisons. The gap 4 pass leaves 3 of its 12 wrong pairs.
D11 comparisons. The gap 4 pass moves nothing, but only 3 pairs are wrong.
pen and paper
5question 5about 15 minutes
Show that Shell sort makes comparisons on numbers already in order, that no row makes fewer, and that this count is .
Count one pass on a row in order, and add up the passes.
Say why no row makes fewer.
Show that at most gaps lie below , and find a ceiling of a fixed multiple of .
a hint
Each gap more than doubles, so compare the gaps with 1, 2, 4, 8 and on.
a worked answer
Each pass compares its numbers once, and nothing moves; on any row each is compared at least once. Gap number is at least , so at most gaps lie below , and the count is at most . For 8 numbers, 11, at most 48.
Why does a row already in order take comparisons, the fewest any row takes, and what ceiling holds?23 of 23 shown
1the move: count one pass on a row in order
the line:
8 numbers in order. 4 comparisons with the gap 4, then 7 with the gap 1, and no move.
why it is allowed: Each number picked up meets a number no bigger places to its left, so it stays put after one comparison. For 8 numbers and the gap 4, places 5 to 8, 4 comparisons.
2the move: list the gaps
the line:
why it is allowed: 3 times 1, and 1, makes 4; 3 times 4, and 1, makes 13. So 8 numbers take the gaps 4 and 1.
guess it, then
3the move: add up the passes
the line:
why it is allowed: is the comparisons on a row in order, lines 1 and 2 added up. For 8 numbers, 7 + 4, which makes 11.
guess it, then
4the move: no row costs fewer
the line:
why it is allowed: On any row each pass picks up the same places, each with a number places to its left, so each is compared at least once.
guess it, then
5the move: so this row is a cheapest one
the line:
why it is allowed: By lines 3 and 4. Others tie, as 2, 1, 3, 4, 5, 6, 7, 8 also costs 11.
guess it, then
6the move: a ceiling for each term
the line:
why it is allowed: Each is less a gap of at least 1. For 8 numbers, 7 and 4.
guess it, then
7the move: add up the ceilings
the line:
why it is allowed: is how many gaps lie below , so by lines 3 and 6, is terms, each less than . For 8 numbers, 11 is at most 2 times 8.
guess it, then
8the move: each gap more than doubles
the line:
The gaps 1, 4 and 13 on a line of powers of two, where each step doubles. Each gap sits more than one step right of the one before.
why it is allowed: By line 2, three times a gap, and 1, is more than twice it.
guess it, then
9the move: compare the gaps with the powers of 2
the line:
gap number
1
1
1
2
4
2
3
13
4
4
40
8
5
121
16
Each gap beside the power of 2 with its number, equal at 1, then ever further above.
why it is allowed: Gap number 1 is 1, 2 to the power 0. Each power of 2 is twice the one before, and each gap more than twice, by line 8. So when one gap is at least its power of 2, the next is too, and so on for ever.
guess it, then
10the move: name the largest gap below n
the line:
why it is allowed: From 2 numbers on the gap 1 is below , and the gaps grow, by line 8. So the gaps below are gap numbers 1 to .
guess it, then
11the move: use line 9 on the largest gap
the line:
why it is allowed: Line 9 with for . For 8 numbers, 4 is at least 2.
guess it, then
12the move: the largest gap is at most n
the line:
why it is allowed: By line 10. For 8 numbers, 4 is at most 8.
guess it, then
13the move: put the two together
the line:
why it is allowed: By lines 11 and 12.
guess it, then
14the move: read it as a log
the line:
why it is allowed: Take the log of both sides of line 13. A bigger number has a bigger log, as the page on halving and logarithms shows, and the log of 2 to a power is that power. For 8 numbers, 1 is at most 3.
guess it, then
15the move: add 1 to both sides
the line:
why it is allowed: Line 14 plus 1.
guess it, then
16the move: multiply both sides by n
the line:
why it is allowed: Line 15 times , above zero. For 8 numbers, 16 is at most 32.
guess it, then
17the move: put the count of gaps in
the line:
why it is allowed: By lines 7 and 16. For 8 numbers, 11 is at most 32.
guess it, then
18the move: a fact for a simpler ceiling
the line:
why it is allowed: From 2 numbers on is at least 2, whose log is 1, and a bigger number has a bigger log.
guess it, then
19the move: add the log to both sides
the line:
why it is allowed: Line 18 plus .
guess it, then
20the move: add like to like
the line:
why it is allowed: Two lots of .
guess it, then
21the move: multiply both sides by n
the line:
why it is allowed: Line 20 times , above zero.
guess it, then
22the move: a simpler ceiling
the line:
why it is allowed: By lines 17 and 21. For 8 numbers, 11 is at most 48.
guess it, then
23the move: read it as a bound
the line:
why it is allowed: By line 22, under twice , a fixed multiple.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
one pass compares n - h times, one term per gap below n
on any row each number picked up is compared at least once
at most gaps below , with a reason
a ceiling such as , with its multiple named
6question 6about 20 minutes
A move of Shell sort trades a number with a smaller one places to its right. Show that each move leaves fewer wrong pairs, so the moves are at most the wrong pairs at the start.
Say what the move does to its own pair.
Take a number between the two, and check every way its size can compare with theirs.
Say why no other pair changes, and finish the argument.
Check it on 9, 4, 8, 1, 6, 3.
a hint
Split it by size, smaller than both, between, bigger than both, or equal to one.
a worked answer
The traded pair is now in order. A number between them keeps one wrong pair with them if smaller or bigger than both, loses two if between them in size, and loses one if equal to one. Other pairs keep their order. So each move leaves at least one fewer, and the sort ends with none. 9, 4, 8, 1, 6, 3 has 11 wrong pairs and takes 9 moves, 2 of them with the gap 4.