OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2
Sorting algorithms
Bubble sort, insertion sort and merge sort. Three ways to put data in order.
Paper 2 · Lesson 18
Les van Computing
In deze les zitten 21 slides, met interactieve quizzen en tekstslides.
OCR GCSE COMPUTER SCIENCE · J277 · PAPER 2
Sorting algorithms
Bubble sort, insertion sort and merge sort. Three ways to put data in order.
Paper 2 · Lesson 18
Lesson 18 of the J277 series (Block C, Paper 2, section 2.1.3 Searching and sorting algorithms).
Cover hologram made from a photo by Kamil / Unsplash: stacks of coins sorted from shortest to tallest.
Device-free: show the slide; pupils write the title and date. Image credit: Kamil / Unsplash (cover hologram)
00 · MISSION · BY THE END OF THIS LESSON
By the end of this lesson you can…
01 Show one pass of a bubble sort and say when it stops.
02 Trace an insertion sort and a merge sort on a list.
03 Compare the three sorts and say when to use each.
Read the three outcomes. J277 expects pupils to understand the main steps of bubble sort, merge sort and insertion sort, apply them to a data set and identify the algorithm from code or a description. They do not need to write the code from memory, but should be able to follow it.
Device-free: pupils copy the outcomes and tick each one at the end.
02 · CHECK · DO NOW (LESSON 17)
Warm up: searching algorithms
1 What must be true of a list before a binary search?
2 Which search checks every item in turn?
3 Which item does a binary search check first?
4 Most checks for a linear search of 50 items?
▮ TERMINAL // do_now_answers
1 it must be sorted
2 a linear search
3 the middle item
4 50 (every item once)
Retrieval from lesson 17. Give 4 minutes, then click to reveal the answers line by line.
1 The list must be sorted (in order). 2 A linear search. 3 The middle item. 4 50: in the worst case the target is last or missing, so every item is checked once.
Link forward: binary search needs sorted data, and today is about how a computer gets data into order in the first place.
Device-free: questions on the board; mini whiteboards.
A binary search needs one thing that a linear search does not. What is it?
The data must be sorted
The list must be short
The data must be numbers
Every item must be unique
Answer: A, the data must be sorted. Binary search throws away half the list after each check, which only works if the items are in order.
A binary search works on words as well as numbers, on long lists as well as short ones, and repeated items do not stop it working. The shelves in the picture are sorted by colour: putting data in order is today's topic.
Photo: Luisa Brimble / Unsplash.
Device-free: students hold up A, B, C or D cards. Image credit: Luisa Brimble / Unsplash
01 · LEARN · MISSION BRIEFING
Put it in order
Leaderboards, playlists and price lists all use a sorting algorithm to put data in order.
BUBBLE SORT
Compare neighbours and swap them if they are in the wrong order.
INSERTION SORT
Slot each item into its place in a sorted part.
MERGE SORT
Split the list up, then merge it back together in order.
SYSTEM VOICE
System online. Today's mission: put data in order. Learn how a bubble sort swaps neighbours, how an insertion sort slots each item into place, and how a merge sort splits a list up and merges it back together.
Click play for the system voice briefing. The words are also on the slide.
Quick hook: ask pupils how they would put 10 exam papers in order of mark. Some will swap pairs, some will pick each paper up and slot it into a pile, some will split the pile between friends and combine the results. Those are the three sorts in this lesson.
Device-free: pupils write the three sort names and a one-line description of each.
01 · LEARN · BUBBLE SORT
Compare and swap neighbours
1 Compare the first two items.
2 Wrong order? Swap them.
3 Move along one place and compare the next pair.
4 At the end of the list, one pass is done.
5 Do more passes until a pass makes no swaps.
No swaps in a pass = sorted.
▮ TERMINAL // bubble_sort.erl
swapped = true
while swapped == true
swapped = false
for i = 0 to items.length - 2
if items[i] > items[i+1] then
temp = items[i]
items[i] = items[i+1]
items[i+1] = temp
swapped = true
endif
next i
endwhile
Click to reveal the steps. A bubble sort goes through the list comparing each pair of neighbouring items and swapping them if they are in the wrong order. One trip through the list is a pass. After each pass the biggest remaining item has "bubbled" to its place at the end.
The terminal shows a bubble sort in OCR Exam Reference Language. Point out: temp holds one value during the swap so it is not lost; swapped records whether anything moved; the while loop stops after a pass with no swaps. Pupils need to recognise this code, not write it from memory.
Device-free: pupils write the five steps in their own words.
01 · LEARN · BUBBLE SORT IN ACTION
Pass 1 on: 6, 3, 8, 2, 5
▮ TERMINAL // bubble_pass_1
6 > 3 ? swap 3 6 8 2 5
6 > 8 ? no 3 6 8 2 5
8 > 2 ? swap 3 6 2 8 5
8 > 5 ? swap 3 6 2 5 8
> 8 is in place. 3 swaps_
MORE PASSES
Pass 2: 3 2 5 6 8
Pass 3: 2 3 5 6 8
Pass 4: no swaps, so it stops. Sorted!
Think: the list was sorted after pass 3. Why does the computer still need pass 4?
Click to run pass 1 one comparison at a time. Each pair of neighbours is compared; if the left item is bigger they swap.
Pass 1: 6 and 3 swap (3 6 8 2 5); 6 and 8 stay; 8 and 2 swap (3 6 2 8 5); 8 and 5 swap (3 6 2 5 8). After one pass the biggest item, 8, is at the end. Pass 2: 3 and 6 stay; 6 and 2 swap; 6 and 5 swap; 6 and 8 stay, giving 3 2 5 6 8. Pass 3: 3 and 2 swap, nothing else moves, giving 2 3 5 6 8. Pass 4: no swaps.
Think: the computer only knows the list is sorted when a whole pass makes no swaps, so it needs one more pass to check.
Device-free: pupils write the list after each comparison of pass 1, then after each pass.
01 · LEARN · INSERTION SORT
Slot each item into place
1 The first item is the sorted part.
2 Take the next item.
3 Compare it with the sorted items, moving left.
4 Shift bigger items one place right and insert it in the gap.
5 Repeat until every item is in the sorted part.
Remember: the sorted part grows by one item each time.
Click to reveal the steps. The picture is the idea to remember: a card player picks up one card at a time and slots it straight into its correct place among the cards already in their hand. That is exactly what an insertion sort does with data, one item at a time.
Stress: the sorted part starts as just the first item and grows by one each time. Items in it may still move to the right to make room, so it is sorted, but not yet final.
Photo: Alessandro Bogliari / Unsplash.
Device-free: give pairs five numbered cards face down. One pupil turns over one card at a time and slots it into the right place in a row. Image credit: Alessandro Bogliari / Unsplash
01 · LEARN · INSERTION SORT IN ACTION
Sort: 4, 7, 2, 9, 1
▮ TERMINAL // insertion_log
start 4 | 7 2 9 1
insert 7 4 7 | 2 9 1
insert 2 2 4 7 | 9 1
insert 9 2 4 7 9 | 1
insert 1 1 2 4 7 9 SORTED
THE | LINE
Left of it is the sorted part. It grows by one each step.
SLOWEST CASE
A list in reverse order: every item moves to the front.
Think: to insert 2, which items had to shift one place to the right?
Click to run the sort one item at a time. Everything left of the | line is the sorted part.
Start: 4 on its own is sorted. Insert 7: 7 is bigger than 4, so it stays. Insert 2: smaller than 7 and 4, so 7 and 4 shift right and 2 goes to the front (2 4 7). Insert 9: bigger than 7, stays (2 4 7 9). Insert 1: smaller than everything, so all four shift right and 1 goes to the front (1 2 4 7 9).
Think: 4 and 7 shifted right to make room for 2.
Device-free: pupils copy the five lines, drawing the | line each time.
01 · LEARN · MERGE SORT
Split it all up, then merge
1 Split the list in half.
2 Keep splitting until each list has one item.
3 Merge pairs of lists, putting the items in order.
4 Keep merging until one list is left.
Merging: compare the front items of two lists and move the smaller one across each time.
Click to reveal the steps. Walk through the diagram from top to bottom.
Split: 7 2 9 4 3 8 6 1 becomes 7 2 9 4 and 3 8 6 1, then 7 2, 9 4, 3 8, 6 1, then eight lists of one item. A list of one item is already sorted. Merge: pairs become 2 7, 4 9, 3 8, 1 6; then 2 4 7 9 and 1 3 6 8; then 1 2 3 4 6 7 8 9.
How to merge 2 4 7 9 with 1 3 6 8: compare the front items (2 and 1), move the smaller (1) into the new list, and repeat: 2, 3, 4, 6, 7, 8, then 9 is left over.
Device-free: pupils copy the diagram for a list of 4 numbers of their choice.
01 · LEARN · COMPARE
Which sort, when?
BUBBLE SORT
+ simple to write
+ little extra memory
– very slow on large lists
INSERTION SORT
+ simple to write
+ quick on small or nearly sorted lists
– slow on large lists
MERGE SORT
+ much faster on large lists
– uses more memory
– harder to write
SYSTEM VOICE · KEY DEFINITION
A merge sort splits a list in half, again and again, until every list holds just one item. Then it merges the lists back together in order, a pair at a time, until one sorted list is left.
The trade-offs examiners look for: merge sort is much faster on large lists, but it needs extra memory to hold the split lists while it merges them, and it is harder to code. Bubble sort and insertion sort sort the list in place, so they need very little extra memory, and they are simple, but they get very slow as the list grows. Insertion sort is usually quicker than bubble sort and works well when the list is small or nearly sorted already.
Play the key definition. Device-free: pupils draw three columns and add one more point to each.
Bubble, insertion or merge sort?
BUBBLE SORT
INSERTION SORT
MERGE SORT
Splits the list into single items
Swaps neighbours in the wrong order
Slots each item into the sorted part
Shifts bigger items one place right
Fast on big lists, but uses more memory
Stops after a pass with no swaps
Answers. Bubble sort: swaps neighbours in the wrong order; stops after a pass with no swaps. Insertion sort: slots each item into the sorted part; shifts bigger items one place right. Merge sort: splits the list into single items; fast on big lists, but uses more memory.
Device-free: pupils draw three columns and write each statement in the right one.
Bubble sort on 5, 1, 4, 2, 8. What is the list after pass 1?
1, 2, 4, 5, 8
1, 5, 4, 2, 8
1, 4, 5, 2, 8
1, 4, 2, 5, 8
Answer: D, 1, 4, 2, 5, 8. Pass 1: 5 and 1 swap (1 5 4 2 8); 5 and 4 swap (1 4 5 2 8); 5 and 2 swap (1 4 2 5 8); 5 and 8 stay. The biggest item, 8, was already at the end.
A is the fully sorted list, which takes more than one pass. B stops after the first swap. C stops after the second swap.
Photo: Jess Bailey / Unsplash. Pencils of different lengths, not yet in order.
Device-free: students hold up A, B, C or D cards. Image credit: Jess Bailey / Unsplash
Insertion sort. The sorted part is 2, 6, 9. Where is 4 inserted?
Before the 2
Between 2 and 6
Between 6 and 9
Right after the 9
Answer: B, between 2 and 6. 4 is compared with 9 (smaller, so 9 shifts right), then with 6 (smaller, so 6 shifts right), then with 2 (bigger, so stop). 4 goes into the gap: 2, 4, 6, 9.
Common wrong answer: A, putting 4 at the front. Only items bigger than 4 shift right; 2 is smaller, so it stays where it is. The coin stacks in the picture are a sorted part: a new stack would be slotted in between the shorter and taller ones.
Photo: Kamil / Unsplash.
Device-free: students hold up A, B, C or D cards. Image credit: Kamil / Unsplash
03 · DEBUG · SPOT THE MISTAKE
One line in Kofi's notes is wrong
▮ TERMINAL // kofi_sorting.txt
1 bubble: compares neighbours
2 bubble: no swaps = sorted
3 insertion: grows a sorted part
4 merge: splits into lists of 2
5 merge: fastest on big lists
Line 4. A merge sort keeps splitting until every list holds ONE item. Then it merges them back together in order.
Check each line against what you know. Then tap the marker.
Tip: a list of one item is already sorted. That is why merge sort splits that far.
Give a minute to find the mistake before tapping the marker.
Answer: line 4. A merge sort splits the list until every list holds one item, not two. A single item cannot be out of order, so the merging can then build sorted lists. Lines 1, 2, 3 and 5 are correct (of the three sorts in this lesson, merge sort is the fastest on large lists).
Device-free: copy the five lines onto the board; pupils write the wrong line number and the correction.
Move on one place and compare the next pair
Swap them if they are in the wrong order
Stop when a pass makes no swaps
Start a new pass from the beginning
Repeat to the end of the list (one pass)
Compare the first two items
Put the steps of a bubble sort in order.
Correct order: 1 compare the first two items; 2 swap them if they are in the wrong order; 3 move on one place and compare the next pair; 4 repeat to the end of the list, which is one pass; 5 start a new pass from the beginning; 6 stop when a pass makes no swaps.
Ask: why can a bubble sort not stop as soon as the list looks sorted? (It only knows when a whole pass makes no swaps.)
Device-free: pupils write the six steps on sticky notes and order them on the desk.
Complete the summary.
A bubble sort compares neighbouring items and swaps them if they are in the wrong order. It stops when a pass makes no swaps. An insertion sort puts each item into its place in the sorted part. A merge sort splits the list into single items, then merges them in order. Merge sort is faster on large lists but uses more memory.
Answers in order: bubble, swaps, insertion, splits, memory. Distractors: binary, searches, colours.
Device-free: paragraph on the board with gaps and the word bank; pupils write the five missing words.
EXAM · Show the stages of a merge sort on the list 3, 7, 5, 1, 8, 2, 6, 4. [4 marks]
Exam-style question, 4 marks. Give 6 minutes. The model answer is on the next slide.
Mark scheme: 1 mark splitting the list in half and in half again until every list holds one item; 1 mark merging into sorted pairs 3 7, 1 5, 2 8, 4 6; 1 mark merging into 1 3 5 7 and 2 4 6 8; 1 mark merging into the final list 1 2 3 4 5 6 7 8.
Do not award the merge marks if the items are only put in order at the end without showing the merges, or if a bubble sort or insertion sort is shown instead.
Device-free: pupils answer on paper, then peer-mark with the next slide.
04 · EXAM · MODEL ANSWER
Split, split, split, merge
▮ TERMINAL // merge_sort_8
split 3 7 5 1 | 8 2 6 4
split 3 7 | 5 1 | 8 2 | 6 4
split 3|7|5|1|8|2|6|4
merge 3 7 | 1 5 | 2 8 | 4 6
merge 1 3 5 7 | 2 4 6 8
merge 1 2 3 4 5 6 7 8
MARK SCHEME
1 mark: split down to single items
1 mark: 3 7, 1 5, 2 8, 4 6
1 mark: 1 3 5 7 and 2 4 6 8
1 mark: 1 2 3 4 5 6 7 8
Show every merge, not just the answer.
Click to reveal the model answer stage by stage.
Split: 3 7 5 1 and 8 2 6 4; then 3 7, 5 1, 8 2, 6 4; then eight single items. Merge: 3 7, 1 5, 2 8, 4 6; then 1 3 5 7 and 2 4 6 8; then 1 2 3 4 5 6 7 8.
Common mistakes: sorting the pairs while splitting (the order only changes when merging), and jumping straight to the final list without showing the merges.
Device-free: pupils check their own answer against the four marks and correct it in a different colour.
How sure are you about sorting?
Very: I can trace all three sorts
Fairly: merge sort is tricky
Not yet: I need another go
Anonymous exit poll. If many pupils find merge sort tricky, start lesson 19 with a quick merge sort of 4 playing cards before moving on.
Device-free: thumbs up, sideways or down.
05 · NEXT · KEEP GOING
Keep going
Isaac Computer Science
Free online lessons and questions. Look for the sorting algorithms topic.
BBC Bitesize
GCSE Computer Science, OCR: revise searching and sorting algorithms.
Sort a deck
Shuffle 8 playing cards. Sort them with a bubble sort, then an insertion sort. Which took fewer moves?
Next lesson: Boolean logic, the AND, OR and NOT gates and truth tables.
Point pupils to the places to practise. Isaac Computer Science and BBC Bitesize are free.
Sort a deck: with a shuffled hand of 8 cards, pupils try both sorts and count the moves. Insertion sort usually needs fewer comparisons than bubble sort on the same cards.
Device-free: pupils write a list of 8 numbers and show every stage of a merge sort on it, to bring to lesson 19.