Skip to content

Merge sort

ordering

Level 2 of 3

Also called Bottom-up merge sort.

test yourself

Four questions to pick from and two for paper; each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Merge sort works on 6, 3, 8, 1, 5, 2. What is the row just after its fourth merge?

638152
the answer, and why

the answer is D

Round 1 merges three pairs. Merge 4 joins 3, 6 with 1, 8, while 2, 5 waits.

the row: 6, 3, 8, 1, 5, 2111111start222round 142round 26round 32a run, its length insideno partner, carried downthe first runall in one runa merge in round 2, its numbers36181368goes straight in, the other run used up
Merge 4, the only merge of round 2.

if you chose another

  • That is after merge 3, the end of round 1.
  • That is the end, after merge 5.
  • Round 1 merges the last pair too, so 5, 2 becomes 2, 5.

question 2

How many comparisons does merge sort make on 7, 7, 7, 7, 7, 7?

777777
the answer, and why

the answer is B

On a tie the left goes first, so each merge compares its whole left run, 3 in round 1, then 2, then 4.

the row: 7, 7, 7, 7, 7, 7111111start222round 142round 26round 32a run, its length insideno partner, carried downthe first runall in one runa merge in round 3, its numbers777777777777goes straight in, the other run used up
Round 3 compares the left four; the right two follow free.

if you chose another

  • That sends the right run first, as back to front.
  • Equal numbers are still compared, to find the tie.
  • That is a dearest row's count, 16 placed less 5 merges.

question 3

Which is true of PP, the numbers merge sort places on a row of nn?

the answer, and why

the answer is A

Which stretches a round merges depends on nn alone, never on the order, and so does PP.

this tests where the formula comes from, on the learn page

if you chose another

  • Every row of 8 places 24, whatever the order.
  • Not for 7 numbers, where PP is 20, and 7 times the log of 7 is about 19.65.
  • It grows faster, 3 for each number at 8 numbers and 10 at 1,024.

question 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.

the row: 3, 7, 1, 5, 2, 6, 41111111start2221round 143round 27round 32a run, its length insideno partner, carried downthe first runall in one runa merge in round 3, its numbers13572461234567goes straight in, the other run used up
Round 3 merges 1, 3, 5, 7 with 2, 4, 6.

if you chose another

  • In order costs 11, each merge comparing only its left run.
  • Back to front costs 9, the fewest.
  • That costs 13, as its last merge holds the 6 and the 7 in one run.

pen and paper

question 5about 20 minutes

Show that merge sort places the same count, PP, on every row of nn numbers, and that from 2 numbers on PP lies between half of n log⁡2 nn \,\log_2\, n and twice it.

  1. Say which stretches a round merges, and why the order does not matter.
  2. Find a ceiling for PP from the rounds, RR, and a ceiling for RR.
  3. Find a floor for PP from what each round places, and a floor for RR.
a hint

The last run length, 2R−12^{R - 1}, is below nn; the next, 2R2^R, is not.

a worked answer

The stretches depend on nn and the run length alone, so PP does too. Each round places a number at most once, so PP is at most R×nR \times n, with RR at most log⁡2 n+1\log_2\, n + 1. Each round before the last places at least half the row, and the last all of it, so PP is at least R×n2R \times \frac{n}{2}, with RR at least log⁡2 n\log_2\, n. For 6 numbers, 16 lies between about 7.75 and about 31.02.

How many numbers does merge sort place on nn numbers, and how does that grow?
  1. the move: the runs at the start of a round

    the line:
    a round with run length wfinds runs at places 1 to w,w+1 to 2w, and so on\begin{gathered} \text{a round with run length } w \\ \text{finds runs at places } 1 \text{ to } w, \\ w + 1 \text{ to } 2w, \text{ and so on} \end{gathered}
    the row: 31, 7, 49, 12, 56, 23, 421111111start2221round 143round 27round 32a run, its length insideno partner, carried downthe first runall in one run
    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.

  2. the move: cut the round into stretches

    the line:
    the round merges stretchesof 2w places from the left:a left run of w or fewerand a right run of the rest\begin{gathered} \text{the round merges stretches} \\ \text{of } 2w \text{ places from the left:} \\ \text{a left run of } w \text{ or fewer} \\ \text{and a right run of the rest} \end{gathered}

    why it is allowed: By line 1, a merge takes 2w2w places. Only the last stretch can be short, and with ww or fewer it is a left run alone.

  3. the move: what each stretch places

    the line:
    a stretch with a right runplaces all its numbers;a last stretch with noright run places none\begin{gathered} \text{a stretch with a right run} \\ \text{places all its numbers;} \\ \text{a last stretch with no} \\ \text{right run places none} \end{gathered}
    run lengthstretcheswaitsplaced
    11 to 2, 3 to 4, 5 to 6, 7the 7th6
    21 to 4, 5 to 7none7
    41 to 7none7
    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.

  4. the move: which rounds there are

    the line:
    the rounds have run lengths1,2,4 and on, each below n\begin{gathered} \text{the rounds have run lengths} \\ 1, 2, 4 \text{ and on, each below } n \end{gathered}

    why it is allowed: By line 1, places 1 to ww hold one run, so a second run, and so a round, exists exactly when ww is below nn. For 7 numbers, 1, 2 and 4, not 8.

  5. the move: add up the rounds

    the line:
    P=p1+p2+p4+…one term for eachrun length below n\begin{gathered} P = p_1 + p_2 + p_4 + \dots \\ \text{one term for each} \\ \text{run length below } n \end{gathered}

    why it is allowed: PP is the numbers placed in all, pwp_w those in the round with run length ww; one term a round, by line 4. For 7 numbers, 6 + 7 + 7 = 20.

  6. the move: the same on every row

    the line:
    P is the sameon every row\begin{gathered} P \text{ is the same} \\ \text{on every row} \end{gathered}

    why it is allowed: By lines 2 and 3, what a round places depends on nn and ww alone, never on the order. For 8 numbers, 24 on every order.

  7. the move: a ceiling for each term

    the line:
    each term is at most n\text{each term is at most } n

    why it is allowed: By line 3 a round places each number once at most.

  8. the move: add up the ceilings

    the line:
    P≤R×nP \le R \times n

    why it is allowed: RR counts the rounds. By lines 5 and 7, RR terms, each at most nn. For 7 numbers, 20 is at most 21.

  9. the move: number the rounds

    the line:
    round number k hasrun length 2k−1\begin{gathered} \text{round number } k \text{ has} \\ \text{run length } 2^{k - 1} \end{gathered}

    why it is allowed: Round 1 has run length 1, 2 to the power 0, and each round doubles it.

  10. the move: the last round's run length

    the line:
    2R−1≤n2^{R - 1} \le n

    why it is allowed: By line 9 the last round, round RR, has run length 2R−12^{R - 1}, below nn by line 4.

  11. the move: take the log to base 2 of both sides

    the line:
    R−1≤log⁡2 nR - 1 \le \log_2\, n
    numberlog× 2+ 11021428372.81
    7 lies between 4 and 8, its log between 2 and 3.

    why it is allowed: In line 10, the log of 2R−12^{R - 1} is R−1R - 1, and a bigger number has a bigger log. For 7 numbers, 2 is at most about 2.81.

  12. the move: add 1 to both sides

    the line:
    R≤log⁡2 n+1R \le \log_2\, n + 1

    why it is allowed: Line 11 plus 1.

  13. the move: multiply both sides by n

    the line:
    R×n≤n(log⁡2 n+1)R \times n \le n(\log_2\, n + 1)

    why it is allowed: Line 12 times nn, more than zero.

  14. the move: a simpler ceiling

    the line:
    P≤n(log⁡2 n+1)P \le n(\log_2\, n + 1)

    why it is allowed: Lines 8 and 13. For 7 numbers, 20 is at most about 26.65.

  15. the move: from 2 numbers on, the log is at least 1

    the line:
    1≤log⁡2 n for n≥21 \le \log_2\, n \text{ for } n \ge 2

    why it is allowed: The log of 2 is 1, and a bigger number has a bigger log.

  16. the move: add the log to both sides

    the line:
     log⁡2 n+1≤log⁡2 n+log⁡2 nfor n≥2\begin{gathered} \,\log_2\, n + 1 \le \log_2\, n + \log_2\, n \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: Line 15 plus log⁡2 n\log_2\, n.

  17. the move: add like to like

    the line:
    log⁡2 n+1≤2 log⁡2 n for n≥2\log_2\, n + 1 \le 2 \,\log_2\, n \text{ for } n \ge 2

    why it is allowed: In line 16, a log plus the same log is twice it.

  18. the move: multiply both sides by n again

    the line:
    n(log⁡2 n+1)≤2n log⁡2 nfor n≥2\begin{gathered} n(\log_2\, n + 1) \le 2n \,\log_2\, n \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: Line 17 times nn.

  19. the move: a simpler ceiling still

    the line:
    P≤2n log⁡2 n for n≥2P \le 2n \,\log_2\, n \text{ for } n \ge 2

    why it is allowed: Lines 14 and 18. For 7 numbers, 20 is at most about 39.30.

  20. the move: read it as a bound

    the line:
    P∈O(n log⁡2 n)P \in O(n \,\log_2\, n)

    why it is allowed: By line 19, at most 2 times n log⁡2 nn \,\log_2\, n.

  21. the move: the next run length is not below n

    the line:
    n≤2Rn \le 2^R

    why it is allowed: Now a floor. The next run length, 2R2^R by line 9, would make another round by line 4 if it were below nn. For 7 numbers, 7 is at most 8.

  22. the move: halve both sides of line 10

    the line:
    2R−2≤n22^{R - 2} \le \frac{n}{2}

    why it is allowed: Halving both sides keeps it true. For 7 numbers, 2 is at most 3.5.

  23. the move: what a round leaves out

    the line:
    a round places at leastn−w numbers\begin{gathered} \text{a round places at least} \\ n - w \text{ numbers} \end{gathered}

    why it is allowed: By line 3 a round leaves out at most a left run alone, ww places or fewer.

  24. the move: a round before the last has a short run length

    the line:
    a round before the last hasrun length at most n2\begin{gathered} \text{a round before the last has} \\ \text{run length at most } \frac{n}{2} \end{gathered}

    why it is allowed: By line 9 its run length is at most 2R−22^{R - 2}, so by line 22 at most half of nn.

  25. the move: so it places at least half the row

    the line:
    each round before the lastplaces at least n2\begin{gathered} \text{each round before the last} \\ \text{places at least } \frac{n}{2} \end{gathered}

    why it is allowed: By lines 23 and 24, nn less at most half of nn.

  26. the move: the last round places every number

    the line:
    the last round places all n\text{the last round places all } n

    why it is allowed: By lines 4 and 21 its run length is below nn 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.

  27. the move: add up the rounds again, from below

    the line:
    P≥(R−1)×n2+nfor n≥2\begin{gathered} P \ge (R - 1) \times \frac{n}{2} + n \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: Line 5, with lines 25 and 26; with 2 numbers no round comes before the last.

  28. the move: a simpler floor

    the line:
    P≥(R−1)×n2+n2for n≥2\begin{gathered} P \ge (R - 1) \times \frac{n}{2} + \frac{n}{2} \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: In line 27, nn is at least half of nn.

  29. the move: tidy the floor

    the line:
    P≥R×n2 for n≥2P \ge R \times \frac{n}{2} \text{ for } n \ge 2

    why it is allowed: In line 28, R−1R - 1 halves and one more make RR halves.

  30. the move: take the log to base 2 of both sides of line 21

    the line:
    log⁡2 n≤R\log_2\, n \le R

    why it is allowed: The log of 2R2^R is RR, and a bigger number has a bigger log.

  31. the move: multiply both sides by half of n

    the line:
    n2 log⁡2 n≤R×n2\frac{n}{2} \,\log_2\, n \le R \times \frac{n}{2}

    why it is allowed: Line 30 times half of nn.

  32. the move: a simpler floor still

    the line:
    P≥n2 log⁡2 n for n≥2P \ge \frac{n}{2} \,\log_2\, n \text{ for } n \ge 2

    why it is allowed: Lines 29 and 31. For 7 numbers, 20 is at least about 9.83.

  33. the move: read it as a bound

    the line:
    P∈Ω(n log⁡2 n)P \in \Omega(n \,\log_2\, n)

    why it is allowed: By line 32, at least half of n log⁡2 nn \,\log_2\, n.

  34. the move: a ceiling and a floor together

    the line:
    P∈Θ(n log⁡2 n)P \in \Theta(n \,\log_2\, n)

    why it is allowed: Lines 20 and 33 bound PP by the same growth. For 8 numbers, PP is 24, as is 8 times 3.

try it with a small nn: at n=6n = 6, line 5 gives P=16P = 16, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • the stretches of a round, set by place alone
  • a ceiling of R×nR \times n, so at most 2n log⁡2 n2n \,\log_2\, n
  • rounds before the last placing half the row or more, the last all
  • a floor of half of n log⁡2 nn \,\log_2\, n, from RR at least log⁡2 n\log_2\, n

question 6about 15 minutes

Show that merging runs of xx and yy numbers makes at most x+y−1x + y - 1 comparisons, that nn numbers take n−1n - 1 merges, and so that any row costs at most P−(n−1)P - (n - 1). Check it on 5, 1, 4, 2, 3.

  1. Say why at least one number of a merge is placed with no comparison.
  2. Count the merges from the number of runs.
  3. 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 nn runs take n−1n - 1 merges. On 5, 1, 4, 2, 3 the table counts round by round.

roundcomparisonsplacedmerges
1242
2341
3351
in all8134
13 placed less 4 merges is 9, and 8 is at most 9.
What is the most a row of nn can cost, which row costs it, and how does that grow?
  1. the move: a merge places its last number with no comparison

    the line:
    merging runs of x and ynumbers compares atmost x+y−1 times\begin{gathered} \text{merging runs of } x \text{ and } y \\ \text{numbers compares at} \\ \text{most } x + y - 1 \text{ times} \end{gathered}
    the row: 31, 7, 42, 181111start22round 14round 22a run, its length insidethe first runall in one runa merge in round 2, its numbers73118427183142goes straight in, the other run used up
    Round 2 merges 7, 31 with 18, 42 into 7, 18, 31, 42.

    why it is allowed: xx and yy 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.

  2. the move: count the merges

    the line:
    a row of n numberstakes n−1 merges\begin{gathered} \text{a row of } n \text{ numbers} \\ \text{takes } n - 1 \text{ merges} \end{gathered}

    why it is allowed: Each merge turns two runs into one, and a run that waits stays one. So nn runs need n−1n - 1 merges. For 8 numbers, 4 + 2 + 1.

  3. the move: add up the merges

    the line:
    on any row, the comparisonsare at most P−(n−1)\begin{gathered} \text{on any row, the comparisons} \\ \text{are at most } P - (n - 1) \end{gathered}

    why it is allowed: Line 1 for each merge, the numbers adding up to PP by the first ladder's line 5, and line 2 above counting the merges. For 8 numbers, 24 less 7, 17.

  4. the move: when a merge runs to its last pair

    the line:
    merging different numbers,a merge comparesx+y−1 times whenits largest number andits next largest standin different runs\begin{gathered} \text{merging different numbers,} \\ \text{a merge compares} \\ x + y - 1 \text{ times when} \\ \text{its largest number and} \\ \text{its next largest stand} \\ \text{in different runs} \end{gathered}

    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.

  5. the move: the last merge's two parts

    the line:
    the last merge joins places1 to 2R−1 with the rest\begin{gathered} \text{the last merge joins places} \\ 1 \text{ to } 2^{R - 1} \text{ with the rest} \end{gathered}

    why it is allowed: By the first ladder's lines 9, 4 and 21, the last run length, 2R−12^{R - 1}, is below nn 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.

  6. the move: earlier merges stay inside one part

    the line:
    every earlier merge joinsnumbers of one part only\begin{gathered} \text{every earlier merge joins} \\ \text{numbers of one part only} \end{gathered}

    why it is allowed: By the first ladder's line 9, an earlier stretch, 2w2w places, is a power of 2 up to the left part's 2R−12^{R - 1}. So these stretches fill places 1 to 2R−12^{R - 1} exactly, and none crosses. For 7 numbers, 1 to 4 holds two stretches of 2, then one of 4.

  7. the move: each part is merged as a row of its own

    the line:
    each part is mergedjust as a row of itsown would be\begin{gathered} \text{each part is merged} \\ \text{just as a row of its} \\ \text{own would be} \end{gathered}

    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 2R−12^{R - 1}, 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.

  8. the move: build a row where every merge runs to its last pair

    the line:
    deal the numbers fromthe largest down, to theright part and to the leftpart in turn; then thesame again inside each part\begin{gathered} \text{deal the numbers from} \\ \text{the largest down, to the} \\ \text{right part and to the left} \\ \text{part in turn; then the} \\ \text{same again inside each part} \end{gathered}
    the row: 1, 5, 3, 7, 2, 6, 4, 811111111start2222round 144round 28round 32a run, its length insidethe first runall in one runa merge in round 3, its numbers1357246812345678goes straight in, the other run used up
    Round 3 merges 1, 3, 5, 7 with 2, 4, 6, 8, from each run in turn.
    the numbersthe left part getsthe right part gets
    1 to 87, 5, 3, 18, 6, 4, 2
    7, 5, 3, 15, 17, 3
    8, 6, 4, 26, 28, 4
    5, 115
    7, 337
    6, 226
    8, 448
    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 x+y−1x + y - 1. Lines 6 and 7 let each part be dealt the same way.

  9. the move: add up that row's merges

    the line:
    the row of line 8 costsexactly P−(n−1)\begin{gathered} \text{the row of line 8 costs} \\ \text{exactly } P - (n - 1) \end{gathered}

    why it is allowed: Line 8 in every merge, added up as in line 3.

  10. the move: the most any row costs

    the line:
    W=P−(n−1)W = P - (n - 1)

    why it is allowed: WW is the comparisons on a dearest row. Line 3 caps every row, and the row of line 9 reaches the cap.

  11. the move: a ceiling

    the line:
    W≤PW \le P

    why it is allowed: In line 10, n−1n - 1 is not below 0.

  12. the move: a simpler ceiling

    the line:
    W≤2n log⁡2 n for n≥2W \le 2n \,\log_2\, n \text{ for } n \ge 2

    why it is allowed: Line 11 above and the first ladder's line 19.

  13. the move: read it as a bound

    the line:
    W∈O(n log⁡2 n)W \in O(n \,\log_2\, n)

    why it is allowed: By line 12, at most 2 times n log⁡2 nn \,\log_2\, n.

  14. the move: a floor

    the line:
    W≥n2 log⁡2 n−(n−1)for n≥2\begin{gathered} W \ge \frac{n}{2} \,\log_2\, n - (n - 1) \\ \text{for } n \ge 2 \end{gathered}

    why it is allowed: Now a floor, putting the first ladder's line 32 into line 10 above.

  15. the move: take away a little more

    the line:
    W≥n2 log⁡2 n−n for n≥2W \ge \frac{n}{2} \,\log_2\, n - n \text{ for } n \ge 2

    why it is allowed: In line 14, nn is more than n−1n - 1.

  16. the move: from 8 numbers on, the log is at least 3

    the line:
    3≤log⁡2 n for n≥83 \le \log_2\, n \text{ for } n \ge 8

    why it is allowed: The log of 8 is 3, and a bigger number has a bigger log.

  17. the move: multiply both sides by a third of n

    the line:
    n≤n3 log⁡2 n for n≥8n \le \frac{n}{3} \,\log_2\, n \text{ for } n \ge 8

    why it is allowed: Line 16 times a third of nn, as a third of nn times 3 is nn.

  18. the move: take more away still

    the line:
    n2 log⁡2 n−n≥n2 log⁡2 n−n3 log⁡2 nfor n≥8\begin{gathered} \frac{n}{2} \,\log_2\, n - n \ge \frac{n}{2} \,\log_2\, n - \frac{n}{3} \,\log_2\, n \\ \text{for } n \ge 8 \end{gathered}

    why it is allowed: By line 17 the right side takes away at least nn.

  19. the move: put the two together

    the line:
    W≥n2 log⁡2 n−n3 log⁡2 nfor n≥8\begin{gathered} W \ge \frac{n}{2} \,\log_2\, n - \frac{n}{3} \,\log_2\, n \\ \text{for } n \ge 8 \end{gathered}

    why it is allowed: Lines 15 and 18.

  20. the move: tidy the floor

    the line:
    W≥n6 log⁡2 n for n≥8W \ge \frac{n}{6} \,\log_2\, n \text{ for } n \ge 8

    why it is allowed: In line 19, a half less a third is a sixth. For 8 numbers, 17 is at least 4.

  21. the move: read it as a bound

    the line:
    W∈Ω(n log⁡2 n)W \in \Omega(n \,\log_2\, n)

    why it is allowed: By line 20, at least a sixth of n log⁡2 nn \,\log_2\, n from 8 numbers on.

  22. the move: a ceiling and a floor together

    the line:
    W∈Θ(n log⁡2 n)W \in \Theta(n \,\log_2\, n)

    why it is allowed: Lines 13 and 21 bound WW by the same growth.

try it with a small nn: at n=8n = 8, line 10 gives W=17W = 17, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • the last number of a merge placed with no comparison, with a reason
  • n - 1 merges, from n runs to one
  • the sum, P less 1 for each merge
  • 8 comparisons and 13 placed on 5, 1, 4, 2, 3