KS3 · Computer Science
Comparing sorting algorithms
Two friends sort the same shuffled cards: 8, 3, 5, 1. Both finish with 1, 3, 5, 8. One made 12 comparisons, the other just 6. Were they equally good?
Computer Science · Sorting algorithms
Bubble sort, one comparison at a time
Press Next and watch the list change. Keep an eye on the tally: every comparison and every swap is counted.
Before you start: which number do you think will be at the far right after pass 1?
1 REPEAT (each time round is one pass)2 FOR each neighbouring pair, from left to right3 IF left item > right item THEN4 swap the two items5 UNTIL a whole pass makes no swaps6 OUTPUT the list
Variables
Output
Step 1: Here's our jumbled list. Pass 1 starts at the left-hand end. Make your prediction, then press Next.
Use Next and Back to step through. In this version, every pass checks every neighbouring pair.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Bubble sort and insertion sort both put a list in order. Count what each one does along the way and you'll see they are not equally good.
What you need to know
- A sorting algorithm puts the items of a list into order, such as ascending or descending, numerical or alphabetical.
- Different sorting algorithms reach the same sorted result by different steps, so we compare them by the work they do (comparisons and swaps or moves), how quickly they finish and how simple they are.
- Bubble sort: go through the list comparing each pair of neighbouring items and swap them if they're in the wrong order. At the end of each pass, the largest remaining item has bubbled to the end of the unsorted part.
- Bubble sort repeats passes until a whole pass makes no swaps. That no-swap pass is what shows the list is sorted.
- Insertion sort: the first item starts as the sorted part. Take the next item from the unsorted part and insert it into its correct place in the sorted part, moving larger items along one place. Repeat until every item is inserted.
- To trace an algorithm, carry it out step by step and write down the list after each pass or each insertion.
- Both are simple and sort in place. On the same list, insertion sort usually needs fewer comparisons and moves, so it's usually quicker. On a list that's already or nearly in order, both do very little work (bubble sort can stop early). On long, jumbled lists both become slow.
- To choose between them, look at the size of the list and how close it already is to being in order.
The big picture
A sorting algorithm puts a list in order. Bubble sort compares neighbouring pairs and swaps any in the wrong order, pass after pass, until a pass makes no swaps. Insertion sort slides each item from the unsorted part into its place in the sorted part. Both give the same sorted list, but insertion sort usually needs fewer comparisons and moves. On a nearly sorted list both do little work; on a long, jumbled list both are slow. So the better choice depends on the list's size and how close it is to sorted.
Key points
Worked example
Problem
A teacher's list of five quiz scores is nearly in order: [2, 4, 3, 7, 9]. Sort it into ascending order with bubble sort, keeping a tally of comparisons and swaps. Then work out whether insertion sort would do more or less work on this list.
⚠ Watch out
Stopping bubble sort the moment the list looks sorted. Bubble sort can only be sure the list is in order after it has made a whole pass with no swaps, so if the last pass made even one swap, it needs another pass.
Memory hook
Bubbles rise: in bubble sort the biggest item floats to the end, one pass at a time. Insertion is like sorting a hand of cards: pick up the next card and slide it into the right spot.
Check yourself
Cover the page. When does bubble sort stop, and why isn't one pass enough? What happens to each item in insertion sort? When would the two do about the same work?
Flashcards
(15)What does a sorting algorithm do?
How can we compare two sorting algorithms that give the same result?
In bubble sort, which items are compared?
What is a 'pass' in bubble sort?
Where is the largest remaining item after one pass of bubble sort?
When does bubble sort stop?
In insertion sort, what counts as the sorted part at the very start?
In insertion sort, what happens to the next item from the unsorted part?
When does insertion sort stop?
What does it mean to trace a sorting algorithm?
On the same list, which usually does less work: bubble sort or insertion sort?
How much work do the two sorts do on a list that's already, or nearly, in order?
Why are both sorts slow on a long, jumbled list?
What does 'sorting in place' mean?
What two things about a list help you choose between bubble sort and insertion sort?
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 1 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.