study / ordering / merge sort / test yourself / fresh numbers previous: randomised quick sort next: heap sort
fresh numbers The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from merge sort's own run on the numbers shown.
choose one 1 question 1Merge sort works on 6, 3, 8, 1, 5, 2. What is the row just after its fourth merge?
6 3 8 1 5 2
A 3, 6, 1, 8, 2, 5 B 1, 2, 3, 5, 6, 8 C 1, 3, 6, 8, 5, 2 D 1, 3, 6, 8, 2, 5 check my answer
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, 2 1 1 1 1 1 1 start 2 2 2 round 1 4 2 round 2 6 round 3 2 a run, its length inside no partner, carried down the first run all in one run a merge in round 2, its numbers 3 6 1 8 1 3 6 8 goes straight in, the other run used up
Merge 4, the only merge of round 2. if you chose another
A That is after merge 3, the end of round 1. B That is the end, after merge 5. C Round 1 merges the last pair too, so 5, 2 becomes 2, 5. new numbers
2 question 2How many comparisons does merge sort make on 7, 7, 7, 7, 7, 7?
7 7 7 7 7 7
A 7 B 9 C 0 D 11 check my answer
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, 7 1 1 1 1 1 1 start 2 2 2 round 1 4 2 round 2 6 round 3 2 a run, its length inside no partner, carried down the first run all in one run a merge in round 3, its numbers 7 7 7 7 7 7 7 7 7 7 7 7 goes straight in, the other run used up
Round 3 compares the left four; the right two follow free. if you chose another
A That sends the right run first, as back to front. C Equal numbers are still compared, to find the tie. D That is a dearest row's count, 16 placed less 5 merges. new numbers
3 question 3Merge sort works on each of these rows of 1 to 7. On which does it make the most comparisons?
A 1, 2, 3, 4, 5, 6, 7 B 7, 6, 5, 4, 3, 2, 1 C 3, 7, 1, 5, 2, 6, 4 D 4, 6, 2, 7, 1, 5, 3 check my answer
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, 4 1 1 1 1 1 1 1 start 2 2 2 1 round 1 4 3 round 2 7 round 3 2 a run, its length inside no partner, carried down the first run all in one run a merge in round 3, its numbers 1 3 5 7 2 4 6 1 2 3 4 5 6 7 goes straight in, the other run used up
Round 3 merges 1, 3, 5, 7 with 2, 4, 6. if you chose another
A In order costs 11, each merge comparing only its left run. B Back to front costs 9, the fewest. D That costs 13, as its last merge holds the 6 and the 7 in one run. new numbers