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.

1 / 7

Each new row is one stage: three rows of splitting, then three rows of merging.

Step 1 of 7: Start. Seven numbers, nowhere near in order. Merge sort won't try to sort them all in one go..

Exam line: To show a merge sort, write one row for every splitting stage and one row for every merging stage.
Watch out: Splitting never puts anything in order. All the ordering happens while lists are being merged.

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.

1start an empty list: merged
2move lower front item to merged
3repeat until A or B is empty
4copy the rest across, in order
A
B
merged
Press Start to run the first line.
·

Ready when you are — step through one line at a time.

Find the slip

Sam's merge looks fine. One line isn't.

Merge the ordered lists A = [6, 13, 20] and B = [9, 11, 25] into one ordered list.

Sam's answer — which line goes wrong?

Your turn

Merge sort seven animals into alphabetical order

Merge sort this list into alphabetical order: wolf, bear, seal, duck, lion, frog, goat. Choose each missing stage.

  1. Start: [wolf, bear, seal, duck, lion, frog, goat]Seven items, so the first split has an odd number to share out.
  2. missing step
Which line is step 2?

Is it always the best?

Kai's bold claim

Kai has just learned merge sort and announces: "Merge sort is the best sorting algorithm. Full stop."

Which idea is closest to what you think right now?
How sure are you?

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

1Merge sort = split, then merge. The ordering happens only while merging.
2Odd-length list: the first list gets the middle item.
3Merging two lists: compare the fronts, take the lower, repeat; then copy the leftovers.
4A list with no partner at a merging stage is copied over to the next stage.
5Usually faster than bubble sort (especially on large datasets), but harder to write, uses extra memory, and can be slower on small datasets.

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?
Splits it in the middle, again and again, until every item is in a list of its own.
What is a divide and conquer algorithm?
One that breaks a problem into smaller and smaller parts until they are simple enough to solve directly. Merge sort is an efficient example.
Name the two parts of a merge sort.
Splitting, then merging.
Splitting a list with an odd number of items: which list gets the middle item?
The first list, so the first list ends up one item longer.
In one merge, which item moves into the merged list next?
Compare the first items of the two lists; the lower one moves into the next free position.
During a merge, one list becomes empty. What happens next?
Each item still in the other list is placed at the end of the merged list, in order.
At a merging stage, one list has no partner. What happens to it?
It is copied over to the next stage of merging.
When does merge sort stop merging?
When all the lists have been merged into one ordered list.
When are the items actually put in order in a merge sort?
While lists are being merged. Splitting doesn't order anything.
Merge sort or bubble sort: which is usually faster?
Merge sort, especially on large datasets.
Merge sort or bubble sort: which is more complex to write?
Merge sort is more complex to implement.
Why does a merge sort take up extra memory?
New lists are made every time a list is split or two lists are combined.
Why can a simpler algorithm beat merge sort on a small dataset?
Merge sort has the overhead of breaking the data apart. On a small dataset, insertion sort or bubble sort can be faster.

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 posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.