GCSE · Computer Science · OCR · Spec J277
Merge sort
Sorting seven jumbled numbers at once is fiddly. Sorting ONE number? Already done. Merge sort cashes in on that: smash the list apart, then stitch it back in order.
Computer Science · Sorting algorithms
Seven numbers fall apart, then click back together
Step through a whole merge sort. The list splits down to single items, then pairs of lists merge, and the items get put in order as they merge.
Predict before you press Next: after the first split, how many numbers land in each list?
| The lists at this stage |
|---|
| [43, 21, 14, 50, 64, 3, 19] |
Output
Step 1: Start. Seven numbers, nowhere near in order. Merge sort won't try to sort them all in one go.
Each new row is one stage: three rows of splitting, then three rows of merging.
Computer Science · Algorithms
Trace table — dry run the code
Merge A = [4, 6, 7] with B = [2, 5, 9]. Before each Next, guess which number moves.
Ready when you are — step through one line at a time.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Split the list all the way down, then merge it back together in order.
What you need to know
- Merge sort keeps splitting a list into smaller lists, then merges pairs of them, putting items in order as they merge.
- It's divide and conquer: it breaks a problem into smaller and smaller parts until each is simple enough to solve directly.
Have a goQuick one. How much sorting does a list with just one item in it need?
None at all: it's already in order.
One item can't be out of order, so a one-item list is the 'simple enough to solve directly' part. That's where merge sort stops dividing.
- Two parts, always in this order: splitting first, then merging.
- Splitting: halve each list in the middle, again and again, until every item is in a list of its own.
- Odd number of items? In this method the first list takes the middle item, so it ends up one item longer.
Have a goYour mate confidently splits [8, 2, 6, 1, 9] into [8, 2] and [6, 1, 9]. Using our method, what should the two lists be?
[8, 2, 6] and [1, 9]
Five is odd, so the first list takes the middle item, 6. Your mate gave the middle item to the second list instead.
- One merge: compare the first items of two ordered lists, move the lower one into the merged list, and repeat.
- When one list runs out, copy each item left in the other list across, in order.
Have a goMerge [3, 10] with [5, 7, 12]. What's the merged list?
[3, 5, 7, 10, 12]
3 beats 5, then 5 and 7 both beat 10, then 10 beats 12. Now [3, 10] is empty, so 12 is copied across. Skip the copy and 12 goes missing.
- Stage by stage, merge lists in pairs until one ordered list is left; a list with no partner is copied over.
- Compared with bubble sort, it's usually faster, especially on large datasets, but more complex to write.
- Costs to remember: extra memory for all the new lists, and on small datasets simpler algorithms can be faster.
The big picture
Merge sort is a divide and conquer sorting algorithm. It splits a list in the middle again and again until every item is on its own, then merges pairs of lists, comparing the front items and taking the lower one, until one ordered list is left. It is usually faster than bubble sort, especially on large datasets, but it is harder to write, takes extra memory, and can be slower than simpler algorithms on small datasets.
Key points
Worked example
Problem
Show every stage of a merge sort on this list: 38, 12, 55, 7, 26, 41, 3, 19.
⚠ Watch out
Joining two lists end to end and calling it a merge. A merge compares the front items every time and takes the lower one, so [21, 43] and [14, 50] become [14, 21, 43, 50], not [21, 43, 14, 50].
Memory hook
Smash it, then stitch it: smash the list down to single items, then stitch pairs back together, lowest front item first.
Check yourself
Without looking back: write out every splitting stage and every merging stage of a merge sort on [9, 4, 7, 2]. Then say which stage first puts any of the numbers in order.
Flashcards
(13)What does merge sort do to a list first?
What is a divide and conquer algorithm?
Name the two parts of a merge sort.
Splitting a list with an odd number of items: which list gets the middle item?
In one merge, which item moves into the merged list next?
During a merge, one list becomes empty. What happens next?
At a merging stage, one list has no partner. What happens to it?
When does merge sort stop merging?
When are the items actually put in order in a merge sort?
Merge sort or bubble sort: which is usually faster?
Merge sort or bubble sort: which is more complex to write?
Why does a merge sort take up extra memory?
Why can a simpler algorithm beat merge sort on a small dataset?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 9 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.