Bubble Sort
Revise Bubble Sort for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). A bubble sort repeatedly compares adjacent items and swaps them until the list is in order.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
A bubble sort repeatedly passes through a list, comparing each adjacent pair and swapping them if they are in the wrong order.
After the first pass, the largest item has moved to the end of the list — this is why it is called a bubble sort, as large values bubble upwards. Each subsequent pass places the next largest item, so the sorted section at the end grows by one each time.
The algorithm stops when a complete pass makes no swaps, which means the list is sorted. It is simple to understand and to write, but inefficient: for n items it may need n − 1 passes, making it much slower than a merge sort on large lists.
Revision notes
How it works
Compare the first two items and swap them if they are in the wrong order.
Move to the next pair and repeat, all the way to the end. That is one pass. After the first pass the largest item is in its final position at the end.
When it stops
Repeat passes until a complete pass makes no swaps.
No swaps means every adjacent pair is in order, so the whole list is sorted. Each pass can ignore the items already placed at the end.
Efficiency
Simple to understand and implement, and needs little extra memory.
But inefficient: for n items it may need n − 1 passes, each with up to n − 1 comparisons. This makes it far slower than a merge sort on large lists.
Key points
- A bubble sort compares adjacent pairs.
- Pairs in the wrong order are swapped.
- One pass moves the largest item to the end.
- The sorted section grows by one each pass.
- It stops when a pass makes no swaps.
- It is simple but inefficient on large lists.
Worked examples
Example 1
Perform one pass of a bubble sort on 5, 3, 8, 1. [3 marks]
Working
Example 2
Explain how a bubble sort knows the list is sorted. [2 marks]
Working
Example 3
Give one advantage and one disadvantage of a bubble sort. [2 marks]
Working
Common mistakes
Swapping non-adjacent items.
Only adjacent pairs are compared and swapped.
Stopping after one pass.
Passes repeat until one makes no swaps.
Forgetting the largest item is placed first.
After pass one the largest is at the end, not the smallest at the start.
Saying it is efficient because it is simple.
Simplicity and efficiency are different things.
Exam tips
- Show each comparison separately in a trace.
- State that it stops when a pass makes no swaps.
- Remember the largest item settles first.
- Give both an advantage and a disadvantage.
Key terms
- Bubble sort
- Sorting by repeatedly swapping adjacent items.
- Pass
- One complete run through the list.
- Swap
- Exchanging two items that are in the wrong order.
- Adjacent
- Next to each other in the list.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.