Merge sort

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Algorithms › Searching and sorting algorithms

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 11 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

A merge sort splits a list in half again and again until each part holds one item, then merges the parts back together in order. You need to show the splitting and merging stages on a given list, describe how two lists are merged, and compare merge sort with bubble and insertion sort.

Key facts

  • Divide: split the list in half repeatedly until every list has 1 item
  • A list of one item is already sorted
  • Merge: compare the front items of two lists and move the smaller one
  • When one list runs out, copy the rest of the other list across
  • Keep merging pairs of lists until one sorted list is left
  • Pro: fast and efficient on large lists
  • Con: extra memory, harder to code, no quicker on small or already sorted lists

Notes

How a merge sort works

  • Divide: split the list in half, then split each half in half, and keep going until every sub-list has one item. A list of one item is already sorted.
  • Merge: merge pairs of sub-lists into sorted lists of two, then fours, and so on, until there is one sorted list.
  • If a list has an odd number of items, one half gets one more item than the other. Follow any rule the question gives about which side.
  • To recognise it in code: the list is split at a middle position into a left half and a right half, then a loop compares the front items of two lists and adds the smaller to a new list.

How two sorted lists are merged

  • Compare the first item of each list. Move the smaller one into the new list.
  • Compare the new front items and repeat.
  • When one list is empty, copy the rest of the other list across in order (it is already sorted).
  • Example: merging 4, 10, 15 with 3, 11, 12, 20 gives 3, 4, 10, 11, 12, 15, 20.

Worked example: 14, 6, 27, 9, 31, 2, 18, 11

diagram
  • Split: [14, 6, 27, 9] [31, 2, 18, 11], then [14, 6] [27, 9] [31, 2] [18, 11], then eight single items.
  • Merge into pairs: [6, 14] [9, 27] [2, 31] [11, 18].
  • Merge into fours: [6, 9, 14, 27] [2, 11, 18, 31].
  • Final merge: [2, 6, 9, 11, 14, 18, 27, 31].
  • 14, 6, 27, 9, 31, 2, 18, 1114, 6, 27, 931, 2, 18, 1114, 627, 931, 218, 1114627931218116, 149, 272, 3111, 186, 9, 14, 272, 11, 18, 312, 6, 9, 11, 14, 18, 27, 31
    Merge sort: split down to single items (top half), then merge back in order (bottom half)

Advantages and disadvantages

  • Advantage: much more efficient (quicker) than bubble sort or insertion sort for large lists, because it makes far fewer comparisons.
  • Advantage: it takes a similar time whatever the starting order of the list.
  • Disadvantages: it needs extra memory to hold the sub-lists; it is more complex to code; it still does every split and merge even if the list is already sorted, and for small lists it can be slower than the simpler sorts.

How to answer each type of question

Show the stages of a merge sort

3 to 4 marksGrade 6
  1. Split the list into halves, then quarters, down to single items. Show each level.
  2. Merge neighbouring single items into sorted pairs.
  3. Merge the pairs into sorted fours, and so on.
  4. Keep each list in square brackets so the examiner can see the groups.

Example. Show how a merge sort would put these names into alphabetical order: Tom, Ann, Raj, Eve, Kim, Bo, Zac, Liv. [4]

Show the model answerHide the model answer
Split: [Tom, Ann, Raj, Eve] [Kim, Bo, Zac, Liv], then [Tom, Ann] [Raj, Eve] [Kim, Bo] [Zac, Liv], then single items
(1).
Merge: [Ann, Tom] [Eve, Raj] [Bo, Kim] [Liv, Zac]
(1).
[Ann, Eve, Raj, Tom] [Bo, Kim, Liv, Zac]
(1).
[Ann, Bo, Eve, Kim, Liv, Raj, Tom, Zac]
(1).
Tom, Ann, Raj, Eve, Kim, Bo, Zac, LivTom, Ann, Raj, EveKim, Bo, Zac, LivTom, AnnRaj, EveKim, BoZac, LivTomAnnRajEveKimBoZacLivAnn, TomEve, RajBo, KimLiv, ZacAnn, Eve, Raj, TomBo, Kim, Liv, ZacAnn, Bo, Eve, Kim, Liv, Raj, Tom, Zac

Describe how two lists are merged

2 to 3 marksGrade 5
  1. Compare the first item in each list.
  2. Move the smaller into the new list and repeat with the next front items.
  3. When one list is empty, add the rest of the other list.

Example. During a merge sort, the lists [4, 10, 15] and [3, 11, 12, 20] are merged.
Describe how they are merged and give the merged list. [3]

Show the model answerHide the model answer
Compare the first item of each list and move the smaller one into the new list (1). Repeat with the front items until one list is empty, then add the remaining items of the other list (1). Merged list: 3, 4, 10, 11, 12, 15, 20 (1).

Evaluate merge sort for a scenario

2 to 4 marksGrade 7
  1. Link the size of the data to efficiency: merge sort suits large lists.
  2. Give the reason: far fewer comparisons than bubble or insertion sort.
  3. If asked for a drawback, mention extra memory or complexity.

Example. An online shop sorts 2 million product prices every night.
Explain why a merge sort is a suitable choice, and give one drawback. [3]

Show the model answerHide the model answer
Merge sort is much quicker (more efficient) than a bubble sort or an insertion sort on a large list (1) because it needs far fewer comparisons
(1).
Drawback: it needs extra memory to store the sub-lists / it is more complex to program
(1).

Don’t lose marks

  • Merging before the list has been split all the way down to single items.
  • Sorting a whole group in one step instead of merging two groups.
  • Losing or duplicating an item: count the items at every stage.
  • Saying merge sort uses less memory than bubble sort: it uses more.
  • Describing a merge as 'join the lists together' without comparing the front items.

More tips

Memory tricks

  • Merge sort in two words: split, then merge.
  • Sense check: at every merging level, each group must be in order and the total number of items must not change.
  • Merging is like two queues of people sorted by height: you always take the shorter of the two people at the front.

Exam technique

  • Show every level of splitting and merging on its own line; each level is usually a mark.
  • Use brackets to show where each sub-list starts and ends.
  • In 'which sort' questions, mention the size of the data and memory, and link them to the scenario.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 4
    Describe the splitting stage of merge sortThe list is repeatedly divided in half until every sub-list contains one item.
  2. Grade 5
    Merge two sorted lists into oneCompare the front item of each list and move the smaller one across, repeatedly.
  3. Grade 6
    Show every stage of a merge sortShow all the splits down to single items, then each round of merging.
  4. Grade 7
    Give advantages and disadvantages of merge sortMuch faster on large lists, but uses more memory and is harder to code.
  5. Grade 8
    Choose a suitable sort for a scenarioJustify merge sort for large data sets, or another sort when memory or simplicity matters.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

During a merge sort, the sorted lists [2, 8] and [5, 9] are merged into one list. State the merged list.
2, 5, 8, 9
Complete the sentences about the second stage of a merge sort. Use words from this list: deleted, merged, reversed, sorted, split.
In the second stage, pairs of sub-lists are ____________ together. Each new list that is made is ____________.
merged; sorted
A merge sort can put a list of numbers in order. State one type of data, other than numbers, that a merge sort can also put in order.
Names (strings)
Name the process of combining two sorted lists into one sorted list.
Merging
Is this statement about a merge sort true or false? If it is false, correct it.
“A merge sort always splits a list into two halves with exactly the same number of items.”
False. A list with an odd number of items cannot be split into two equal halves.

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
(a) Describe the first stage of a merge sort.
[2]
(b) Which statement about merge sort is true?
Tick (✓) one box.
[1]
  • It only works on lists that are already sorted
  • It is usually more efficient than a bubble sort for large lists
  • It compares each item with the item next to it and swaps them
  • It can only sort numbers, not words
(c) Give one disadvantage of a merge sort compared with a bubble sort.
[1]
Show the answer and mark scheme
(a)
  • the list is repeatedly divided / split in half
  • until each sub-list contains only one item
(b) Answer: It is usually more efficient than a bubble sort for large lists
(c)
  • uses more memory (to store the sub-lists)
  • more complex to code / understand
  • carries out all the splitting and merging even if the list is already sorted
  • can be slower for very small lists
Question 2Medium5 marks
A merge sort is used to sort a list of toys into alphabetical order. The list has already been split into single items:
[kite] [drum] [yoyo] [ball] [top] [doll] [sled] [bike]
(a) Show the lists after each stage of merging.
[3]
(b) Give one advantage and one disadvantage of merge sort compared with bubble sort.
[2]
Show the answer and mark scheme
(a) Answer: [drum, kite] [ball, yoyo] [doll, top] [bike, sled]
[ball, drum, kite, yoyo] [bike, doll, sled, top]
[ball, bike, doll, drum, kite, sled, top, yoyo]
  • [drum, kite] [ball, yoyo] [doll, top] [bike, sled]
  • [ball, drum, kite, yoyo] [bike, doll, sled, top]
  • [ball, bike, doll, drum, kite, sled, top, yoyo]
(b)
  • advantage: more efficient / quicker for large lists
  • disadvantage: uses more memory / is more complex to program
Question 3Hard7 marks
The function below merges two sorted arrays, a and b, into one sorted array. Lines 21 to 25 are missing.
01 function merge(a, b)
02    array result[a.length + b.length]
03    i = 0
04    j = 0
05    k = 0
06    while i < a.length AND j < b.length
07       if a[i] <= b[j] then
08          result[k] = a[i]
09          i = i + 1
10       else
11          result[k] = b[j]
12          j = j + 1
13       endif
14       k = k + 1
15    endwhile
16    while i < a.length
17       result[k] = a[i]
18       i = i + 1
19       k = k + 1
20    endwhile
21    ............
22    ............
23    ............
24    ............
25    ............
26    return result
27 endfunction
(a) The function is called with a = [4, 10, 13] and b = [6, 7].
State the values of i, j and k when the loop on lines 06 to 15 ends.
[2]
(b) Write the missing code for lines 21 to 25.
[3]
(c) Explain whether the function would still produce a correctly sorted array if line 07 used < instead of <=.
[2]
Show the answer and mark scheme
(a) Answer: i = 1, j = 2, k = 3
  • i = 1 and j = 2
  • k = 3
(b) Answer:
while j < b.length
   result[k] = b[j]
   j = j + 1
   k = k + 1
endwhile
  • a loop that runs while j < b.length
  • copies b[j] into result[k]
  • increases both j and k by 1 inside the loop
(c)
  • yes, the result would still be sorted
  • when a[i] and b[j] are equal it does not matter which is placed first, because the other one is placed straight after it

More in Searching and sorting algorithms

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.