Sorting algorithms: bubble, merge and insertion sort
10 questions44 marksabout 55 min
Answer on paper first, then reveal the mark scheme one mark at a time.
1Grade 4–54 marks
A bubble sort is used to put this list into ascending order.
[6, 2, 9, 1, 5]
(a) Show the list after the first pass. [2]
(b) Show the list after the second pass. [1]
(c) State how a bubble sort knows that the list is sorted. [1]
(a) 9 has moved to the end(1)
(a) Completely correct: [2, 6, 1, 5, 9](1)
(b) [2, 1, 5, 6, 9](1)
(c) A complete pass is made with no swaps; allow: when the swapped flag is still false at the end of a pass; allow: after n − 1 (here 4) passes have been made(1)
Answer: (a) [2, 6, 1, 5, 9]; (b) [2, 1, 5, 6, 9]; (c) a pass with no swaps
Examiner tip: One pass compares every adjacent pair once. Don't show the list after a single swap and call that a pass.
A program needs to sort a list of 50 000 customer records.
Compare merge sort and bubble sort for this task. [4]
Merge sort is more efficient / quicker than bubble sort for a large list such as 50 000 records(1)
…because merge sort (divide and conquer) makes far fewer comparisons, while bubble sort makes repeated passes, each comparing up to 49 999 adjacent pairs; allow: bubble sort's time grows much faster as the list gets bigger(1)
Any one from: bubble sort is simpler to code / understand; bubble sort sorts in place so needs little extra memory; bubble sort can stop early (no swaps) if the list is already (nearly) sorted(1)
Any one further point from: merge sort needs extra memory to hold the sub-lists; merge sort is harder to code (often recursive); a second different point from the bubble sort list above. Do not credit the same idea twice (e.g. 'bubble sort uses less memory' and 'merge sort uses more memory' = one point)(1)
Answer: Merge sort is much faster on 50 000 records because it makes far fewer comparisons, but it needs extra memory and is harder to code. Bubble sort is simpler, sorts in place and can stop early on a nearly sorted list, but is very slow on a large list.
Examiner tip: A compare question needs points about both algorithms, ideally linked with 'whereas'. Writing the same point twice from each side (e.g. less memory / more memory) only earns one mark.
1. Repeatedly compares each pair of neighbouring items and swaps them if they are in the wrong order, until a whole pass is made with no swaps.
2. Splits the list in half again and again until each list holds one item, then combines the lists back together in order.
3. Takes each item in turn and slides it leftwards into the correct position in the part of the list that is already sorted.
(a) Write the name of the sorting algorithm that matches each description. [3]
(b) State why merge sort is described as a divide and conquer algorithm. [1]
(a) 1 = bubble sort(1)
(a) 2 = merge sort(1)
(a) 3 = insertion sort(1)
(b) It breaks the problem into smaller sub-problems (halves the list / splits it into smaller lists) and solves each smaller part before combining the results; allow: it splits (divides) the list and then merges the parts back together(1)
Answer: (a) 1 bubble, 2 merge, 3 insertion; (b) it splits the list into smaller and smaller lists, sorts those and combines them
Examiner tip: Read the key verb in each description: 'swaps neighbours' is bubble, 'splits in half' is merge, 'slides into the sorted part' is insertion. Mixing up bubble and insertion is the common slip.
A procedure is written in OCR Exam Reference Language.
procedure sort(list)
for i = 1 to list.length - 1
current = list[i]
j = i - 1
while j >= 0 AND list[j] > current
list[j + 1] = list[j]
j = j - 1
endwhile
list[j + 1] = current
next i
endprocedure
(a) Identify the sorting algorithm that this procedure carries out. [1]
(b) Give two features of the code that show it is this algorithm. [2]
(c) The procedure is called with list = [5, 2, 4]. State the contents of list at the end of the first iteration of the for loop (when i = 1). [1]
(a) Insertion sort(1)
(b) Any one of: one item (current) is taken out / held at a time; the items in the sorted part are shifted right (list[j + 1] = list[j]) rather than swapped in pairs; the inner loop moves backwards (j = j - 1) through the already-sorted part of the list(1)
(b) A second different feature from the list above, e.g. current is put into the gap (list[j + 1] = current) once a smaller item is found; the outer loop starts at index 1 because the first item counts as an already-sorted list of one(1)
(c) [2, 5, 4](1)
Answer: (a) Insertion sort; (b) e.g. one item is held while larger items are shifted right, and the inner loop works backwards through the sorted part; (c) [2, 5, 4]
Examiner tip: For (b) quote the code, not just the algorithm's definition: 'it uses a sorted part' earns nothing unless you point to the lines that shift items right and work backwards.
Two students each sort the list [7, 3, 8, 1, 5] into ascending order using a different standard algorithm. They record the list at the end of each pass.
Student A
Student B
Pass 1
[3, 7, 8, 1, 5]
[3, 7, 1, 5, 8]
Pass 2
[3, 7, 8, 1, 5]
[3, 1, 5, 7, 8]
Pass 3
[1, 3, 7, 8, 5]
[1, 3, 5, 7, 8]
Pass 4
[1, 3, 5, 7, 8]
[1, 3, 5, 7, 8]
(a) Identify the algorithm used by Student A and give one reason from the table for your answer. [2]
(b) Identify the algorithm used by Student B and give one reason from the table for your answer. [2]
(a) Insertion sort(1)
(a) Reason, any one of: the sorted part grows by one item from the left each pass (3,7 then 3,7,8 then 1,3,7,8); an item jumps straight to its place (1 moves from index 3 to index 0 in a single pass); pass 2 makes no change because 8 is already larger than the sorted part; the largest value (8) is not at the end after pass 1(1)
(b) Bubble sort(1)
(b) Reason, any one of: the largest value moves to the end after pass 1 (8), then the next largest (7) after pass 2; small values move only one place left per pass (1 goes from index 3 to 2 to 1 to 0) because only neighbouring items are swapped; pass 4 is identical to pass 3 because a pass with no swaps is needed to stop(1)
Answer: (a) Insertion sort – the sorted section at the left grows by one item each pass; (b) bubble sort – the largest value is bubbled to the end on each pass
Examiner tip: Look at the right-hand end: if the biggest value is already at the end after one pass it is bubble sort. If a small value has jumped several places to the left in one pass it is insertion sort.
(b) Describe how the merge step decides which item to place next in the merged list. [2]
(c) State the number of comparisons made between items during this merge. [1]
(d) Explain why a merge sort still carries out every split and merge even when the original list is already in order. [1]
(a) [4, 6, 8, 11, 19, 25, 27, 30](1)
(b) Compares the first (smallest remaining) item of each sub-list and copies the smaller one into the merged list; allow: a pointer / index for each sub-list is moved on after its item is taken(1)
(b) Repeats until one sub-list is empty, then the rest of the other sub-list is copied across (here 27, 30); allow: only one item is removed from a sub-list at each comparison(1)
(c) 6(1)
(d) Merge sort has no check for whether the list is already sorted / it always divides down to single items and merges them back regardless of the data, so it does the same work for any order; allow: unlike bubble sort it has no 'no swaps' stopping condition(1)
Answer: (a) [4, 6, 8, 11, 19, 25, 27, 30]; (b) compare the first item of each sub-list, take the smaller, repeat; when one sub-list is empty copy the rest of the other; (c) 6; (d) it has no check for an already-sorted list – it always splits and merges everything
Examiner tip: In (c) do not count the items copied after one sub-list runs out – 27 and 30 are moved without a comparison, so the answer is 6, not 8.
Examiner tip: Descending order means swap when the left item is smaller. Students often sort ascending out of habit – re-read the stem before the first comparison.
The diagram shows a merge sort being used to sort the list [21, 5, 17, 9, 30, 2, 14, 11] into ascending order. The split stage is complete. Three of the boxes in the merge stage, labelled A, B and C, have been left blank.
(a) Write the contents of boxes A, B and C. [3]
(b) This list of 8 items needs 3 rounds of splitting to reach single items. State the number of rounds of splitting needed for a list of 32 items and explain your answer. [2]
(c) Explain why a merge sort usually needs more memory than a bubble sort while it is running. [1]
(a) A = [2, 30](1)
(a) B = [2, 11, 14, 30](1)
(a) C = [2, 5, 9, 11, 14, 17, 21, 30](1)
(b) 5(1)
(b) Because each round halves the list: 32 → 16 → 8 → 4 → 2 → 1 / because 25 = 32 (allow: log2 32 = 5)(1)
(c) Merge sort creates new (sub-)lists / copies of the data at each merge, so extra storage is needed alongside the original list, whereas bubble sort swaps items within the original list (sorts in place)(1)
Answer: (a) A = [2, 30]; B = [2, 11, 14, 30]; C = [2, 5, 9, 11, 14, 17, 21, 30]; (b) 5, because the list is halved each round and 25 = 32; (c) merge sort stores extra sub-lists while merging, bubble sort swaps in place
Examiner tip: Box B merges the two sorted pairs directly above it (A and [11, 14]), not a re-sort of the original right half – build it by comparing the first remaining item of each pair in turn.