CNot for 7 numbers, where is 20, and 7 times the log of 7 is about 19.65.
DIt grows faster, 3 for each number at 8 numbers and 10 at 1,024.
4question 4
Merge sort works on each of these rows of 1 to 7. On which does it make the most comparisons?
the answer, and why
the answer is C
In each merge the two largest stand in different runs, so it compares all but its last number, 20 placed less 6 merges, 14.
Round 3 merges 1, 3, 5, 7 with 2, 4, 6.
if you chose another
AIn order costs 11, each merge comparing only its left run.
BBack to front costs 9, the fewest.
DThat costs 13, as its last merge holds the 6 and the 7 in one run.
pen and paper
5question 5about 20 minutes
Show that merge sort places the same count, , on every row of numbers, and that from 2 numbers on lies between half of and twice it.
Say which stretches a round merges, and why the order does not matter.
Find a ceiling for from the rounds, , and a ceiling for .
Find a floor for from what each round places, and a floor for .
a hint
The last run length, , is below ; the next, , is not.
a worked answer
The stretches depend on and the run length alone, so does too. Each round places a number at most once, so is at most , with at most . Each round before the last places at least half the row, and the last all of it, so is at least , with at least . For 6 numbers, 16 lies between about 7.75 and about 31.02.
How many numbers does merge sort place on numbers, and how does that grow?34 of 34 shown
1the move: the runs at the start of a round
the line:
Runs of 1, then 2, 2, 2 and 1, then 4 and 3, then 7.
why it is allowed: Round 1 starts from runs of 1, and merging in pairs from the left doubles them, as drawn. The last run may be shorter.
2the move: cut the round into stretches
the line:
why it is allowed: By line 1, a merge takes places. Only the last stretch can be short, and with or fewer it is a left run alone.
guess it, then
3the move: what each stretch places
the line:
run length
stretches
waits
placed
1
1 to 2, 3 to 4, 5 to 6, 7
the 7th
6
2
1 to 4, 5 to 7
none
7
4
1 to 7
none
7
The rounds on 7 numbers, 6 + 7 + 7 = 20 placed.
why it is allowed: A merge writes each of its numbers into the second row once. A left run alone waits; the Python copies it across, but no merge places it.
guess it, then
4the move: which rounds there are
the line:
why it is allowed: By line 1, places 1 to hold one run, so a second run, and so a round, exists exactly when is below . For 7 numbers, 1, 2 and 4, not 8.
guess it, then
5the move: add up the rounds
the line:
why it is allowed: is the numbers placed in all, those in the round with run length ; one term a round, by line 4. For 7 numbers, 6 + 7 + 7 = 20.
guess it, then
6the move: the same on every row
the line:
why it is allowed: By lines 2 and 3, what a round places depends on and alone, never on the order. For 8 numbers, 24 on every order.
guess it, then
7the move: a ceiling for each term
the line:
why it is allowed: By line 3 a round places each number once at most.
guess it, then
8the move: add up the ceilings
the line:
why it is allowed: counts the rounds. By lines 5 and 7, terms, each at most . For 7 numbers, 20 is at most 21.
guess it, then
9the move: number the rounds
the line:
why it is allowed: Round 1 has run length 1, 2 to the power 0, and each round doubles it.
guess it, then
10the move: the last round's run length
the line:
why it is allowed: By line 9 the last round, round , has run length , below by line 4.
guess it, then
11the move: take the log to base 2 of both sides
the line:
7 lies between 4 and 8, its log between 2 and 3.
why it is allowed: In line 10, the log of is , and a bigger number has a bigger log. For 7 numbers, 2 is at most about 2.81.
guess it, then
12the move: add 1 to both sides
the line:
why it is allowed: Line 11 plus 1.
guess it, then
13the move: multiply both sides by n
the line:
why it is allowed: Line 12 times , more than zero.
guess it, then
14the move: a simpler ceiling
the line:
why it is allowed: Lines 8 and 13. For 7 numbers, 20 is at most about 26.65.
guess it, then
15the move: from 2 numbers on, the log is at least 1
the line:
why it is allowed: The log of 2 is 1, and a bigger number has a bigger log.
guess it, then
16the move: add the log to both sides
the line:
why it is allowed: Line 15 plus .
guess it, then
17the move: add like to like
the line:
why it is allowed: In line 16, a log plus the same log is twice it.
guess it, then
18the move: multiply both sides by n again
the line:
why it is allowed: Line 17 times .
guess it, then
19the move: a simpler ceiling still
the line:
why it is allowed: Lines 14 and 18. For 7 numbers, 20 is at most about 39.30.
guess it, then
20the move: read it as a bound
the line:
why it is allowed: By line 19, at most 2 times .
guess it, then
21the move: the next run length is not below n
the line:
why it is allowed: Now a floor. The next run length, by line 9, would make another round by line 4 if it were below . For 7 numbers, 7 is at most 8.
guess it, then
22the move: halve both sides of line 10
the line:
why it is allowed: Halving both sides keeps it true. For 7 numbers, 2 is at most 3.5.
guess it, then
23the move: what a round leaves out
the line:
why it is allowed: By line 3 a round leaves out at most a left run alone, places or fewer.
guess it, then
24the move: a round before the last has a short run length
the line:
why it is allowed: By line 9 its run length is at most , so by line 22 at most half of .
guess it, then
25the move: so it places at least half the row
the line:
why it is allowed: By lines 23 and 24, less at most half of .
guess it, then
26the move: the last round places every number
the line:
why it is allowed: By lines 4 and 21 its run length is below and twice it is not, so by lines 2 and 3 it merges the whole row. For 7 numbers, 1 to 4 with 5 to 7.
guess it, then
27the move: add up the rounds again, from below
the line:
why it is allowed: Line 5, with lines 25 and 26; with 2 numbers no round comes before the last.
guess it, then
28the move: a simpler floor
the line:
why it is allowed: In line 27, is at least half of .
guess it, then
29the move: tidy the floor
the line:
why it is allowed: In line 28, halves and one more make halves.
guess it, then
30the move: take the log to base 2 of both sides of line 21
the line:
why it is allowed: The log of is , and a bigger number has a bigger log.
guess it, then
31the move: multiply both sides by half of n
the line:
why it is allowed: Line 30 times half of .
guess it, then
32the move: a simpler floor still
the line:
why it is allowed: Lines 29 and 31. For 7 numbers, 20 is at least about 9.83.
guess it, then
33the move: read it as a bound
the line:
why it is allowed: By line 32, at least half of .
guess it, then
34the move: a ceiling and a floor together
the line:
why it is allowed: Lines 20 and 33 bound by the same growth. For 8 numbers, is 24, as is 8 times 3.
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
rounds before the last placing half the row or more, the last all
a floor of half of , from at least
6question 6about 15 minutes
Show that merging runs of and numbers makes at most comparisons, that numbers take merges, and so that any row costs at most . Check it on 5, 1, 4, 2, 3.
Say why at least one number of a merge is placed with no comparison.
Count the merges from the number of runs.
Add up, then count 5, 1, 4, 2, 3 round by round.
a hint
Each merge turns two runs into one, and a run that waits stays one run.
a worked answer
A merge stops comparing when a run is used up, so its last number follows with none. Each merge leaves one run fewer, so runs take merges. On 5, 1, 4, 2, 3 the table counts round by round.
round
comparisons
placed
merges
1
2
4
2
2
3
4
1
3
3
5
1
in all
8
13
4
13 placed less 4 merges is 9, and 8 is at most 9.
What is the most a row of can cost, which row costs it, and how does that grow?22 of 22 shown
1the move: a merge places its last number with no comparison
the line:
Round 2 merges 7, 31 with 18, 42 into 7, 18, 31, 42.
why it is allowed: and are the runs' lengths. Each comparison places one number. The comparison that uses up one run leaves a number in the other, which follows with none. Merging 7, 31 with 18, 42: 3 comparisons.
2the move: count the merges
the line:
why it is allowed: Each merge turns two runs into one, and a run that waits stays one. So runs need merges. For 8 numbers, 4 + 2 + 1.
guess it, then
3the move: add up the merges
the line:
why it is allowed: Line 1 for each merge, the numbers adding up to by the first ladder's line 5, and line 2 above counting the merges. For 8 numbers, 24 less 7, 17.
guess it, then
4the move: when a merge runs to its last pair
the line:
why it is allowed: The next largest ends its run, so that run lasts until all but the largest are placed. The largest waits in the other run all along, so each placing took a comparison. Merging 7, 31 with 18, 42: 3.
guess it, then
5the move: the last merge's two parts
the line:
why it is allowed: By the first ladder's lines 9, 4 and 21, the last run length, , is below and twice it is not. So by the first ladder's line 2 the last stretch is the whole row. For 8 numbers, 1 to 4 and 5 to 8.
guess it, then
6the move: earlier merges stay inside one part
the line:
why it is allowed: By the first ladder's line 9, an earlier stretch, places, is a power of 2 up to the left part's . So these stretches fill places 1 to exactly, and none crosses. For 7 numbers, 1 to 4 holds two stretches of 2, then one of 4.
guess it, then
7the move: each part is merged as a row of its own
the line:
why it is allowed: By line 6 each part is cut as a row of its own. Its own rounds have run lengths 1, 2, 4 and on, below its length, by the first ladder's line 4. Each part holds at most , by line 5, so these come before the last round, by the first ladder's line 9. In other rounds a short part waits, as the 9th of 9 does.
guess it, then
8the move: build a row where every merge runs to its last pair
the line:
Round 3 merges 1, 3, 5, 7 with 2, 4, 6, 8, from each run in turn.
the numbers
the left part gets
the right part gets
1 to 8
7, 5, 3, 1
8, 6, 4, 2
7, 5, 3, 1
5, 1
7, 3
8, 6, 4, 2
6, 2
8, 4
5, 1
1
5
7, 3
3
7
6, 2
2
6
8, 4
4
8
Largest first, right first; the bottom four lines give 1, 5, 3, 7, 2, 6, 4, 8.
why it is allowed: By line 5 the right part is never the longer; once it is full, the rest go left. The two largest stand apart, so by line 4 the last merge makes . Lines 6 and 7 let each part be dealt the same way.
guess it, then
9the move: add up that row's merges
the line:
why it is allowed: Line 8 in every merge, added up as in line 3.
guess it, then
10the move: the most any row costs
the line:
why it is allowed: is the comparisons on a dearest row. Line 3 caps every row, and the row of line 9 reaches the cap.
guess it, then
11the move: a ceiling
the line:
why it is allowed: In line 10, is not below 0.
guess it, then
12the move: a simpler ceiling
the line:
why it is allowed: Line 11 above and the first ladder's line 19.
guess it, then
13the move: read it as a bound
the line:
why it is allowed: By line 12, at most 2 times .
guess it, then
14the move: a floor
the line:
why it is allowed: Now a floor, putting the first ladder's line 32 into line 10 above.
guess it, then
15the move: take away a little more
the line:
why it is allowed: In line 14, is more than .
guess it, then
16the move: from 8 numbers on, the log is at least 3
the line:
why it is allowed: The log of 8 is 3, and a bigger number has a bigger log.
guess it, then
17the move: multiply both sides by a third of n
the line:
why it is allowed: Line 16 times a third of , as a third of times 3 is .
guess it, then
18the move: take more away still
the line:
why it is allowed: By line 17 the right side takes away at least .
guess it, then
19the move: put the two together
the line:
why it is allowed: Lines 15 and 18.
guess it, then
20the move: tidy the floor
the line:
why it is allowed: In line 19, a half less a third is a sixth. For 8 numbers, 17 is at least 4.
guess it, then
21the move: read it as a bound
the line:
why it is allowed: By line 20, at least a sixth of from 8 numbers on.
guess it, then
22the move: a ceiling and a floor together
the line:
why it is allowed: Lines 13 and 21 bound by the same growth.
guess it, then
try it with a small : at , line 10 gives , which you can work out by hand