KS3 · Computer Science
Bubble sort
Sort a shuffled row of cards, but you may only ever look at two neighbours at a time. Impossible? Bubble sort does exactly that.
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. Keep an eye on 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. Wrong way round? Swap them. Move along one place. That tiny rule, repeated pass after pass, sorts a whole list.
What you need to know
- A sorting algorithm puts a list in order: ascending (smallest to largest), descending (largest to smallest) or alphabetical.
- Bubble sort starts at the beginning of the list and compares adjacent (neighbouring) pairs of items.
- If a pair is in the wrong order, swap the two items. Then move on one place to the next pair.
- 'Wrong order' depends on the goal: for ascending, swap when the left item is bigger; for descending, when it's smaller.
Have a goYour classmate is putting 2, 7 into DESCENDING order and announces, very confidently: "No swap needed. 2 is smaller, so it goes first." Are they right?
No. Swap them, giving 7, 2.
In descending order the bigger number belongs on the left, so a smaller left item means the pair is the wrong way round; "smaller goes first" is the ascending rule.
- One complete run through the list, from start to end, is called a pass.
- After each pass, the largest unsorted item has 'bubbled' to its correct place at the end of the unsorted part.
Have a goPass 1 has just finished on a list of five different numbers, sorting into ascending order. Which position is now guaranteed to hold its final number?
The last position: it now holds the largest number.
Each pass carries the largest unsorted item to the end; nothing guarantees the smallest has reached the front yet.
- Passes repeat until one is completed with no swaps. That swap-free pass shows the list is sorted, so the algorithm stops.
Have a goA sort's swap counts, pass by pass, read 3, 2, 0. How many passes did it make, and could the last one have been skipped?
3 passes, and no, the last one couldn't be skipped.
Even if the list was already in order after pass 2, that pass made swaps; only the pass with 0 swaps proves every pair is in order.
- To trace a bubble sort by hand, write the list after each comparison or each pass, and record the swaps made.
- Simple to understand and write, bubble sort is slow (inefficient) on long lists: many passes, very many comparisons and swaps.
- Nearly sorted list? Bubble sort is quicker on it, because it reaches its pass with no swaps early.
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 on long lists, and 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
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 October 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.