All subjects · Computer Science · WCP03 Advanced Principles of Computer Science

Merge sort

Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science

Merge sort is a divide and conquer algorithm that is fast and reliable on any data. You must trace it by hand and explain how it works; in Unit 4 you implement it recursively.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

How it works

1. Divide: split the list in half, again and again, until every sub-list has one item (a list of one item is already sorted).

2. Conquer and combine: merge pairs of sorted sub-lists into larger sorted lists until one sorted list remains.

Merging two sorted lists: compare the first items of each; move the smaller to the output; repeat; when one list runs out, copy the rest of the other.

Trace

Sort [38, 27, 43, 3, 9, 82, 10, 15].

StageLists
Split to single items38 | 27 | 43 | 3 | 9 | 82 | 10 | 15
Merge level 1[27, 38] | [3, 43] | [9, 82] | [10, 15]
Merge level 2[3, 27, 38, 43] | [9, 10, 15, 82]
Merge level 3[3, 9, 10, 15, 27, 38, 43, 82]

Recursive code

def merge_sort(items):
    if len(items) <= 1:
        return items                    # base case
    mid = len(items) // 2
    left = merge_sort(items[:mid])
    right = merge_sort(items[mid:])
    return merge(left, right)

def merge(left, right):
    out, i, j = [], 0, 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            out.append(left[i]); i += 1
        else:
            out.append(right[j]); j += 1
    return out + left[i:] + right[j:]

Efficiency

There are about log2 n levels of splitting, and each level of merging handles all n items, so the time complexity is O(n log n) in the best, average and worst case.

Benefits: consistently fast, even on large or reverse-ordered lists; stable (equal items keep their order). Drawbacks: needs extra memory O(n) for the merged lists; slower than simpler sorts on very small lists; no faster on data that is already sorted.

Worked example

Merge the sorted lists [3, 27, 38, 43] and [9, 10, 15, 82].

  1. 3 vs 9: take 3. 27 vs 9: take 9. 27 vs 10: take 10. 27 vs 15: take 15.
  2. 27 vs 82: take 27. 38 vs 82: take 38. 43 vs 82: take 43.
  3. Left list is empty: copy 82.

Answer: [3, 9, 10, 15, 27, 38, 43, 82] using 7 comparisons

Key idea: Merge sort: split to single items, then merge sorted pairs. O(n log n) in every case, stable, but needs O(n) extra memory.

Check yourself

  1. Merge sort is applied to [64, 25, 12, 22, 11, 90, 47, 5]. Give the four sub-lists after the first level of merging, as numbers separated by commas in order.
    Show answer

    25, 64, 12, 22, 11, 90, 5, 47. Pairs are merged: [[25, 64], [12, 22], [11, 90], [5, 47]].

  2. For the same list, give the two sub-lists after the second level of merging, as numbers separated by commas in order.
    Show answer

    12, 22, 25, 64, 5, 11, 47, 90. [[12, 22, 25, 64], [5, 11, 47, 90]].

  3. How many comparisons are made when merging [2, 5, 9] with [3, 4, 10]?
    Show answer

    5. 2v3 take 2; 5v3 take 3; 5v4 take 4; 5v10 take 5; 9v10 take 9; then copy 10: 5 comparisons.

  4. How many levels of merging are needed to sort 32 items with merge sort?
    Show answer

    5. log2 32 = 5.

  5. What is the worst-case time complexity of merge sort?
    • O(n log n)
    • O(n2)
    • O(n)
    • O(log n)
    Show answer

    O(n log n). Every case is O(n log n).

  6. Which is a drawback of merge sort?
    • Its worst case is O(n2)
    • It only works on numbers
    • It needs extra memory to hold the merged lists
    • It cannot be written recursively
    Show answer

    It needs extra memory to hold the merged lists. Typical implementations need O(n) extra space.

  7. What is the base case of a recursive merge sort?
    • A list with zero or one items, which is returned unchanged
    • A list of two items, which is swapped
    • When the list is half sorted
    • When the pivot is found
    Show answer

    A list with zero or one items, which is returned unchanged. A single item is already sorted.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.