KS3 · Computer Science
Bubble sort
Imagine sorting a shuffled row of cards when you may only ever look at two neighbouring cards at a time. Could you still do it? Bubble sort can.
Computing · Algorithms
Watch the 9 bubble to the end
Bubble sort putting 9 3 7 2 into ascending order, one comparison at a time. It only ever looks at two neighbours.
Predict first: after pass 1, which number will be sitting at the end of the list?
1 REPEAT2 swaps = 03 FOR each pair of neighbours, left to right4 IF left item > right item THEN5 swap the two items6 swaps = swaps + 17 ENDIF8 NEXT pair9 UNTIL swaps = 0
| pass | pair | swap? | list | swaps |
|---|---|---|---|---|
| 9 3 7 2 |
Output
Step 1: Here's the list: 9 3 7 2. The goal is ascending order, smallest to largest. Keep your eye on the 9. It's the biggest, and it starts right at the front.
Press Next to make one comparison at a time. Watch the pair, the list column and the swaps count.
Predict, then check
Both lists hold the same five numbers. The only difference is how jumbled they are.
Bubble sort puts each list into ascending order. List A is 2 1 3 4 5. List B is 5 4 3 2 1. Which one finishes in fewer passes?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Look at two neighbours. Swap them if they're the wrong way round. Move along one place. That tiny rule, repeated pass after pass, sorts a whole list.
What you need to know
- A sorting algorithm puts the items in a list into order: ascending (smallest to largest), descending (largest to smallest) or alphabetical.
- Bubble sort compares adjacent (neighbouring) pairs, starting at the beginning of the list, swaps a pair if it is in the wrong order, then moves on one place to the next pair.
- One complete run through the list from start to end is a pass. After each pass, the largest unsorted item has bubbled to its place at the end of the unsorted part.
- Bubble sort repeats passes until a pass makes no swaps. A pass with no swaps shows the list is sorted, so the algorithm stops.
- To trace a bubble sort by hand, write the list after each comparison or each pass, and record the swaps made.
- Bubble sort is simple to understand and write, but slow (inefficient) for long lists. It is quicker when the list is nearly sorted.
The big picture
A sorting algorithm puts a list into order: ascending, descending or alphabetical. Bubble sort compares neighbouring pairs from the start of the list, swaps any pair that is the wrong way round, and moves on one place. One run through the list is a pass, and each pass carries the largest unsorted item to the end of the unsorted part. Bubble sort keeps making passes until a pass makes no swaps, which shows the list is sorted. It is simple to understand and write, but slow for long lists; it is quicker when the list is nearly sorted.
Key points
Worked example
Problem
Use bubble sort to put 4, 8, 1, 6 into descending order (largest first). Write the list after each pass and the number of swaps in each pass, and say when the algorithm stops.
⚠ Watch out
Forgetting that the list has changed after a swap. After swapping, the next comparison is between the item that has just moved and its new neighbour, not the pair from the original list.
Memory hook
Two neighbours, wrong way round? Swap them and step along. Pass after pass the big ones bubble to the end, and the first pass with zero swaps says STOP.
Check yourself
Bubble sort 7, 2, 5 into ascending order, writing the list and the swaps after each pass. How many passes does it make, and why not stop after pass 1?
Flashcards
(13)What does a sorting algorithm do?
Ascending or descending: which is smallest to largest?
What is the basic move in bubble sort?
What is a pass?
In an ascending bubble sort, what is certain after each pass?
When does bubble sort stop?
Why can't bubble sort stop as soon as the list looks sorted?
Sorting into descending order: when do two neighbours get swapped?
Sorting words alphabetically: when do two neighbours get swapped?
How do you record a bubble sort traced by hand?
Give one strength of bubble sort.
Why is bubble sort slow on a long list?
When is bubble sort quicker?
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 postedMore KS3 Computer Science topics
- Abstraction in computational thinking
- Adding binary numbers
- Binary to denary conversion
- Boolean logic: AND, OR, NOT
- Building truth tables
- Client-server vs peer-to-peer
- Collecting and recording data
- Comparing sorting algorithms
- Compressing data
- Creating a 3D animation
- Decomposition: splitting problems up
- Designing a mobile app
How this lesson was checked. This KS3 Computer Sciencelesson 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 30 September 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.