Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Merge Sort

3.1.4 Sorting algorithms

Aligned to the AQA 8525 specification

Level
Intermediate
Reading time
8 min
Published
2 June 2026
Updated
1 July 2026
On this page
  1. 1.What Merge Sort Does
  2. 2.The Split Phase
  3. 3.The Merge Phase: Combining Two Sorted Lists
  4. 4.Merging Multi-Element Sub-lists
  5. 5.Merge Sort: Full Worked Example
  6. 6.Merge Sort vs Bubble Sort: Efficiency
  7. 7.Common Exam Mistakes

Key takeaways

  • Merge sort uses a divide-and-conquer strategy: it splits a list into smaller pieces, then merges those pieces back together in sorted order.
  • The split phase does no comparisons; it only divides the list until every sub-list holds one element, which is sorted by definition and acts as the base case.
  • Merging combines two already-sorted sub-lists by repeatedly taking the smaller of the two front elements, appending the rest once one sub-list is exhausted.
  • Merge sort runs in O(n log n) in both best and worst cases, far faster than bubble sort's O(n squared) on large datasets.
  • Merge sort needs extra memory proportional to n for its sub-lists, whereas bubble sort is in-place and uses no extra allocations.

What Merge Sort Does

Merge sort is a sorting algorithm that uses a divide-and-conquer strategy: it splits a list into smaller pieces, and then merges those pieces back together in sorted order.

Unlike bubble sort — which repeatedly compares adjacent elements across the whole list — merge sort never compares elements from the full unsorted list directly. Every merge step takes two already-sorted sub-lists and combines them into one sorted output.

Merge sort runs in two distinct phases:

  1. Split — divide the list in half repeatedly until every sub-list contains exactly one element
  2. Merge — combine pairs of sorted sub-lists into progressively larger sorted lists, until the full list is reassembled

A list of one element is sorted by definition — this is the base case that terminates the splitting.

The Split Phase

Starting with the full list, merge sort divides it in half at each level until every sub-list contains a single element.

Example — split [38, 12, 45, 9, 22, 7]:

After splitting: [38] [12] [45] [9] [22] [7]

Each sub-list has one element and is trivially sorted. The split phase produces levels of division. For 6 elements: levels.

The split phase does no comparisons — it only divides the list. All the comparison work happens during the merge phase.

The Merge Phase: Combining Two Sorted Lists

Merging takes two sorted sub-lists and produces one sorted output by repeatedly selecting the smaller of the two front elements.

Merging [12] and [45]:

Left remainingRight remainingActionOutput so far
1245Take 12 (smaller)[12]
45Left exhausted — take all of right[12, 45]

Result: [12, 45]

Merging [22] and [7]:

Left remainingRight remainingActionOutput so far
227Take 7 (smaller)[7]
22Right exhausted — take all of left[7, 22]

Result: [7, 22]

When one sub-list is exhausted, all remaining elements of the other sub-list are appended in order — no further comparisons are needed because that sub-list is already sorted.

Merging Multi-Element Sub-lists

Single-element merges are straightforward. Merging two multi-element sorted sub-lists follows the same rule — always take the smaller front element — but requires more steps.

Merging [9] and [7, 22]:

Left remainingRight remainingActionOutput so far
97, 22Take 7 (smaller)[7]
922Take 9 (smaller)[7, 9]
22Left exhausted — take all of right[7, 9, 22]

Result: [7, 9, 22]

Merging [38] and [12, 45]:

Left remainingRight remainingActionOutput so far
3812, 45Take 12 (smaller)[12]
3845Take 38 (smaller)[12, 38]
45Left exhausted — take all of right[12, 38, 45]

Result: [12, 38, 45]

The key point: once one sub-list is exhausted, the remaining elements of the other sub-list are appended without further comparisons, because they are already in order.

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

Merge Sort: Full Worked Example

Sort [38, 12, 45, 9, 22, 7] using merge sort.

Split phase (top-down) produces six single-element sub-lists: [38] [12] [45] [9] [22] [7]

Merge phase (bottom-up, following the split tree):

StepSub-lists being mergedResult
1[12] + [45][12, 45]
2[38] + [12, 45][12, 38, 45]
3[22] + [7][7, 22]
4[9] + [7, 22][7, 9, 22]
5[12, 38, 45] + [7, 9, 22][7, 9, 12, 22, 38, 45]

Step 5 in detail — merging [12, 38, 45] and [7, 9, 22]:

Left remainingRight remainingActionOutput so far
12, 38, 457, 9, 22Take 7[7]
12, 38, 459, 22Take 9[7, 9]
12, 38, 4522Take 12[7, 9, 12]
38, 4522Take 22[7, 9, 12, 22]
38, 45Right exhausted — append left[7, 9, 12, 22, 38, 45]

Final sorted list: [7, 9, 12, 22, 38, 45]

Merge Sort vs Bubble Sort: Efficiency

Both algorithms sort a list, but their performance on large datasets differs enormously.

PropertyMerge sortBubble sort
Worst-case comparisons
Best-case comparisons (optimised, already sorted)
For ~700~10,000
For ~130,000~100,000,000
Extra memory neededYes — sub-lists during mergeNo — sorts in place

means the number of comparisons grows proportionally to multiplied by . means it grows with the square of — far faster.

An optimised bubble sort implementation includes an early-exit flag that detects when no swaps occurred in a pass, allowing it to terminate in a single pass on an already-sorted list ( best case). Without this optimisation, bubble sort always runs passes. Merge sort always performs the full split-and-merge regardless of initial order.

For large datasets, merge sort is consistently faster than bubble sort. The trade-off is memory: merge sort needs additional space proportional to n for the sub-lists during merging, while bubble sort sorts in place.

Common Exam Mistakes

1. Describing the merge before the split

The split always comes first. The entire point of splitting is to produce single-element lists that are trivially sorted — without the split, there are no sorted sub-lists to merge.

2. Confusing the merge step with a re-sort

Merging works correctly because both input sub-lists are already sorted. The algorithm takes the smaller front element from either list — it does not sort either sub-list during the merge. Answers that say "arrange the elements in order" miss this mechanism.

3. Getting the number of split levels wrong

For elements the number of split levels is . For 8 elements: levels exactly. For 6 elements: , so levels.

4. Stating merge sort uses less memory than bubble sort

The opposite is true. Merge sort requires additional memory proportional to n for the sub-lists created during merging. Bubble sort is in-place — it uses only the original list with no extra allocations.

5. Claiming bubble sort is always slower

An optimised bubble sort with an early-exit flag runs in on an already-sorted list; merge sort always runs . On small or nearly-sorted datasets, an optimised bubble sort can outperform merge sort. The comparison depends on the data and whether the early-exit optimisation is implemented.

Key terms

Merge sort
A sorting algorithm that splits a list into single elements then merges them back together in sorted order.
Divide-and-conquer
A strategy that breaks a problem into smaller pieces, solves each, then combines the results.
Split phase
The stage that divides the list in half repeatedly until each sub-list holds one element, doing no comparisons.
Merge phase
The stage that combines pairs of sorted sub-lists into progressively larger sorted lists.
Base case
A single-element list, which is sorted by definition and terminates the splitting.
In-place
Sorting that uses only the original list with no extra memory allocations, as bubble sort does.

Frequently asked questions

Split and merge. The split phase divides the list in half repeatedly until every sub-list contains a single element, doing no comparisons. The merge phase then combines pairs of sorted sub-lists by repeatedly taking the smaller front element, until the full list is reassembled in order.

On large datasets, yes: merge sort runs in O(n log n) while bubble sort runs in O(n squared). But an optimised bubble sort with an early-exit flag runs in O(n) on an already-sorted list, so on small or nearly-sorted data it can outperform merge sort.

More. Merge sort requires extra memory proportional to n for the sub-lists created during merging. Bubble sort is in-place, using only the original list with no extra allocations, so claiming merge sort uses less memory is wrong.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Next

Variables, Constants and Data Types

Related lessons

6 min

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.