ScholaFly

CS11-05 Computer Science Watch

Merge sort

Watch on YouTube

In this lesson

Merge sort: how to describe and apply a merge sort, with worked examples and the mistakes examiners report. AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Searching and sorting 5 of 7. More at https://scholafly.com

By the end: Describe and apply a merge sort - split the list in half again and again until every list holds one item, then merge pairs of lists back together, taking the smaller front item each time so that every merged list is in order - and state that the merging is what does the sorting.

What it covers

  1. 0:00 Intro: merge sort
  2. 0:41 Merge sort: merging two sorted lists
  3. 2:30 Merge sort: the full run
  4. 5:12 What is wrong with this student's merge?
  5. 6:40 Merge sort in words

Key words

About this video

Merge sort | GCSE Computer Science | CS11-05

Merge sort: how to describe and apply a merge sort, with worked examples and the mistakes examiners report.
AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Searching and sorting 5 of 7.
More at https://scholafly.com

Watch first: CS11-04 - https://youtu.be/iM0KFXiNPp0
Video code: CS11-05 (search "ScholaFly CS11-05")

#GCSEComputerScience #SearchingAndSorting #MergeSort

For teachers
This GCSE Computer Science lesson teaches merge sort. By the end, students should be able to describe and apply a merge sort - split the list in half again and again until every list holds one item, then merge pairs of lists back together, taking the smaller front item each time so that every merged list is in order - and state that the merging is what does the sorting. It works through four worked examples and the mistakes examiners report.

Exam board specification references:
AQA 8525
- 3.1.4
Edexcel 1CP2
- 1.2.6
OCR J277
- 2.1.3

Read the transcript

Two friends each hold a pile of exam papers, lowest mark on top. How do they make one ordered pile without sorting anything? Tipping them together and re-sorting is tempting, but wastes the order they have. Just take the lower top paper, again and again. A whole sorting method rests on that one move. So where does its sorting actually happen? Keep that last question handy; the full run, in the middle of this video, answers it.

Two sorted lists can be joined into one sorted list, and that join is called a merge. Compare the front items of the two lists. Move the smaller one into the new list. Then compare the new front items, and keep going until both lists are empty. Merge seven, twelve, thirty-eight, forty-five with three, eighteen, twenty-one, twenty-nine. The fronts are seven and three. Three is smaller, so three goes first. The fronts are now seven and eighteen, and seven goes next. The fronts are now twelve and eighteen. Which two items go into the new list next? Twelve, then eighteen. Twelve is smaller than eighteen, and then eighteen is smaller than thirty-eight. Then thirty-eight against twenty-one: twenty-one moves. Thirty-eight against twenty-nine: twenty-nine moves. The second list is empty, so thirty-eight and forty-five follow, in order. Three, seven, twelve, eighteen, twenty-one, twenty-nine, thirty-eight, forty-five. The merge built it in order, one front item at a time, and nothing was sorted afterwards.

A full merge sort uses that move again and again, after first breaking the list down. The list is thirty-eight, twelve, forty-five, seven, twenty-nine, three, eighteen, twenty-one. Split it in half, into two lists of four. Split each half again, into lists of two, and then again, into eight lists of one item each. A list of one item is already sorted, because there is nothing for it to be out of order with. That is why the splitting stops there. Now merge the single items back in pairs. What are the four lists of two? Twelve, thirty-eight. Seven, forty-five. Three, twenty-nine. Eighteen, twenty-one. Each pair goes smaller first. Then merge those into lists of four. Twelve, thirty-eight with seven, forty-five gives seven, twelve, thirty-eight, forty-five. Three, twenty-nine with eighteen, twenty-one gives three, eighteen, twenty-one, twenty-nine. Those two lists of four are the pair merged a moment ago. One more merge, and the list is sorted. Down, the lists go eight, four, two, one. Up, they go one, two, four, eight. Splitting is called the divide stage and merging the conquer stage, so merge sort uses divide and conquer, like binary search. One examiner's report describes the best answers this way: The strongest responses clearly showed both the divide stage, splitting values up into individual lists, and then the conquer stage, merging lists together in order. The habit that matches it is to box every list, and to write one line for every level going down and one for every level going up.

Now a student's merge to check. They merge two single items into one list of two, then, in a later step, sort that list. Seventeen and minus twenty-seven merged to seventeen, minus twenty-seven: what is wrong? Minus twenty-seven is the smaller front value, so it goes first. Done properly, that step gives minus twenty-seven, then seventeen. And the later sort step should not exist. Every merge builds its list in order, front item by front item, so there is never anything left to sort. One examiner's report puts it like this: However, several candidates still merge lists together into an unsorted list and then take a second step to sort the list. This has been commented on in previous Examiner's Reports but remains a common misconception. The fix is one habit: build each merged list by comparing front items, so it is in order the moment it is finished. If an answer ever needs a sort step after a merge, the merge went wrong.

Some questions ask for the steps in words, with no diagram at all. Take a list of six items. Six splits into two lists of three, and three cannot split evenly. Pick a rule and keep to it. Here, whenever a list cannot halve evenly, the left part takes the extra one. Write the merge sort steps for six items in words, split then merge. Pause and work it out. Split six into three and three. Split each three into two and one, and each two into one and one. That leaves six lists of one item. Merge each pair of single items into a list of two, then each two with its one into a list of three, then the two threes into six. Every merge compares front items, so every list it makes is in order. Another examiner's report says why every level has to be shown. No mark was given simply for showing the final list sorted unless the previous steps had been followed. It adds that an alternative sorting algorithm, for example a bubble sort, would achieve zero marks even though the final outcome is the same. So show every level, down and up, because the final list alone says nothing about how you got there.

Fresh numbers now, for splitting, merging and that missing sort step. Merge four, nine with two, eleven. Which item goes into the new list first, and why? Two. The fronts are four and two, and two is smaller. Now finish that merge: what is the full merged list? Two, four, nine, eleven. Four goes in before eleven, then nine, and eleven comes last. A different question: how many splits take a list of four down to single items? Two levels. Four splits into two lists of two, and each of those splits into two lists of one. Now the two piles of exam papers: why is no sort step ever needed? Each pile starts in order, and taking the lower top paper every time builds the new pile in order as it grows.

If merge sort has sunk in, a thumbs up marks it, and you can walk past it next time you revise. Should it still feel shaky, save it for later; the video comparing bubble sort and merge sort brings merging straight back round.

Next in the chapter: Insertion sort, a third sort, on one course's list. Hopefully that's next in your feed, but if not, search for ScholaFly C S eleven, oh six.

Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.

Related terms

For: AQA GCSE 8525, Edexcel GCSE 1CP2, OCR GCSE J277

On the specification

BoardSpecStatement
AQA GCSE 85253.1.4Sorting algorithms
Edexcel GCSE 1CP21.2.6Algorithms
OCR GCSE J2772.1.3Searching and sorting algorithms
For teachers

This GCSE Computer Science lesson teaches merge sort. By the end, students should be able to describe and apply a merge sort - split the list in half again and again until every list holds one item, then merge pairs of lists back together, taking the smaller front item each time so that every merged list is in order - and state that the merging is what does the sorting. It works through four worked examples and the mistakes examiners report.