ScholaFly

CS11-04 Computer Science Watch

Bubble sort

Watch on YouTube

In this lesson

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

By the end: Describe and apply a bubble sort - compare each pair of neighbouring items, swap them if they are in the wrong order, repeat across the list as one pass, and keep making passes until a pass makes no swaps - showing every swap, and recognise it from code.

What it covers

  1. 0:00 Intro: bubble sort
  2. 0:40 Bubble sort
  3. 3:44 When to stop
  4. 5:43 A sorted list
  5. 7:24 Bubble sort in code

Key words

About this video

Bubble sort | GCSE Computer Science | CS11-04

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

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

#GCSEComputerScience #SearchingAndSorting #BubbleSort

For teachers
This GCSE Computer Science lesson teaches bubble sort. By the end, students should be able to describe and apply a bubble sort - compare each pair of neighbouring items, swap them if they are in the wrong order, repeat across the list as one pass, and keep making passes until a pass makes no swaps - showing every swap, and recognise it from code. It works through three 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

A class sorts itself by height: walking along the line, any two neighbours swap if the taller one is on the left. When can they stop? When the tallest reaches the end is tempting, and one walk gets them there. But that settles only the tallest. And nobody may step back to look, so the signal to stop must come from the walk itself. The signal to stop turns up before this video ends, and it is a simple one.

Putting a list in order is called sorting, and a bubble sort does it by comparing neighbours. Neighbours are two items next to each other. Compare them, and if they are in the wrong order, swap them. Here the order is ascending, which means smallest first. Then move along by one place and compare the next pair. Going from the start of the list to the end like that is called one pass. After one pass, the largest item is in its final place at the end. It wins every comparison it takes part in, so each swap carries it one step further along. The steps, then, are compare neighbours, swap if they are in the wrong order, move along one, and at the end of each pass, check whether anything was swapped. The list is six, two, nine, four, one, and the sort puts it in ascending order. Every swap gets the list written out again. Six and two are in the wrong order, so they swap. The list is now two, six, nine, four, one. Six and nine are already in the right order, so they stay put, and the comparison moves along one place. Nine and four come next. What does the list look like after this comparison? Two, six, four, nine, one. Nine is larger than four, so the two swap. Last pair of the pass: nine and one, wrong order, swap. Two, six, four, one, nine. Pass one is over, with three swaps, and nine has reached the end. On completing a bubble sort, one examiner's report records over sixty-four per cent gaining full marks. The same report adds a warning: Some students completed a full pass rather than showing each change which limited the marks available. The fix: when a question asks for the steps, each swap is a step. Write the list out again every time two items change places.

One pass is not enough on this list, so the passes repeat. Pass two starts from two, six, four, one, nine. Six swaps with four, then six swaps with one. The list becomes two, four, one, six, nine, after two swaps. Pass three: four swaps with one. Two, one, four, six, nine, after one swap. Pass four starts from two, one, four, six, nine. What does pass four produce? One, two, four, six, nine. Two and one swap, and nothing else moves. Now the list is in order. Does the bubble sort stop here, after pass four? No. Pass four made a swap, so the sort cannot yet know the list is sorted. It needs one more pass. Pass five compares every neighbour pair and swaps nothing. A pass with no swaps proves every pair is in order, so the algorithm stops there. That is the stopping rule: keep making passes until a pass makes no swaps at all. Not after one pass, and not after a fixed number of them.

Some lists are finished after a single pass of a bubble sort. Take three, five, eight, twelve, already in ascending order. How many passes does a bubble sort make on that list, and why that many? One pass. It compares each neighbour pair, swaps nothing, and a pass with no swaps means stop. That is the best case for bubble sort: a list that is already sorted, so the first pass makes no swaps and the sort ends after one pass. Now one answer: best case is when fewest comparisons are needed. What's wrong with it? It says the question back, and gives no reason at all. One examiner's report records the same: Many candidates reiterated the wording of the question stating that the best case was when the fewest comparisons were needed. Responses that reiterated the question were not creditworthy. The fix is to say what happens on this list. Already sorted, so the first pass makes no swaps, so one pass ends it.

Code for a bubble sort has three signs, and none of them is its name. Two loops, one inside the other. A comparison of an item with the one right after it, written i and i plus one. And three lines that swap two values using a variable called temp. Which three lines of this code do the swap? Lines six, seven and eight. Temp keeps one value safe while the other is copied across. Temp is short for temporary. Swapping two drinks between two glasses needs a spare cup for a moment, and temp is that cup. The variable swapped is how the code knows to stop. It is set to False at the start of each pass, turns True on any swap, and the outer loop ends after a pass that leaves it False. One examiner's report records how students did on code like this: Only around thirty per cent of the students were able to identify that it was a bubble sort with one common misconception being that due to a variable being called temp that the algorithm was to do with temperature. The fix is to read the shape, not the names. Two loops, neighbours compared, a three-line swap: that is a bubble sort, whatever its variables are called.

The four steps of a bubble sort, compare, swap, move along and stop, get tested on fresh lists now. Pass one on five, three, eight, two: what is the list after the first swap? Three, five, eight, two. Five and three were the wrong way round, so they swapped. Now finish that pass: where does eight end up, and why? Last in the list. It is the largest, so it wins every comparison it is in, and is carried to the end. A different list now: its last swap comes in pass three. When does the sort stop? After pass four. Pass three still made a swap, so pass four runs, swaps nothing, and the sort stops. Try the class lined up by height: when do you stop walking the line? Once a whole walk goes by with nobody swapping places. That walk proves every pair of neighbours is in order.

If you have got it, tap the thumbs up and this video is ticked off for good. If the passes are still a blur, save it for later; two or three lists in, they start to feel routine.

Next in the chapter: Merge sort, a second way to put a list in order. Hopefully that's next in your feed, but if not, search for ScholaFly C S eleven, oh five.

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 bubble sort. By the end, students should be able to describe and apply a bubble sort - compare each pair of neighbouring items, swap them if they are in the wrong order, repeat across the list as one pass, and keep making passes until a pass makes no swaps - showing every swap, and recognise it from code. It works through three worked examples and the mistakes examiners report.