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 right
3 IF left item > right item THEN
4 swap the two items
5 UNTIL a whole pass makes no swaps
6 OUTPUT the list

Variables

list=[8, 3, 5, 1]pass=1swaps=0comparisons=0

Output

 

Step 1: Here's our jumbled list. Pass 1 starts at the left-hand end. Make your prediction, then press Next.

1 / 18

Use Next and Back to step through. In this version, every pass checks every neighbouring pair.

Step 1 of 18: Here's our jumbled list. Pass 1 starts at the left-hand end. Make your prediction, then press Next..

Watch out: A list can look sorted before bubble sort has finished. It only stops after a pass that makes no swaps.

Your turn · Insertion sort

Same list, different algorithm

Sort [8, 3, 5, 1] again, this time with insertion sort. The bar | splits the list: the sorted part is on the left, the unsorted part on the right. Choose the missing list states, and keep a tally of comparisons and moves.

  1. Start: [8 | 3, 5, 1]The first item, 8, counts as a sorted part all on its own. Tally: 0 comparisons, 0 moves.
  2. Insert 3 → [3, 8 | 5, 1]Take 3. Compare it with 8: 8 is bigger, so 8 moves one place right. Nothing is left to compare with, so 3 drops in at the front. Tally: 1 comparison, 1 move.
  3. missing step
Which line is step 3?

Check your thinking

What did those two traces show?

Bubble sort and insertion sort both turned [8, 3, 5, 1] into [1, 3, 5, 8].

Which of these is closest to what you think right now?
How sure are you?

Bubble sort vs insertion sort

Bubble sortvsInsertion sort

Start at the top: the first row is the one people most often get wrong.

Focus

The final result

Bubble sort

[8, 3, 5, 1] becomes [1, 3, 5, 8]

Insertion sort

[8, 3, 5, 1] becomes [1, 3, 5, 8]

The insight

Identical. So 'which is better?' can never be about the answer. It has to be about the work each one does.

How it works

Bubble sort

Compares neighbouring pairs and swaps any in the wrong order, pass after pass

Insertion sort

Takes the next unsorted item and slides it into its place in the sorted part

Work on [8, 3, 5, 1]

Bubble sort

12 comparisons, 5 swaps

Insertion sort

6 comparisons, 5 moves

A list already in order: [1, 3, 5, 8]

Bubble sort

One pass, 3 comparisons, no swaps, then it stops early

Insertion sort

3 comparisons, no moves: every item is already in its place

A long, jumbled list

Bubble sort

Back-to-front, 4 items: 12 comparisons. 8 items: 56

Insertion sort

Back-to-front, 4 items: 6 comparisons. 8 items: 28

Make the call

Is insertion sort always the better choice?

The claim

Insertion sort is always the better choice, whatever the list.

Place each piece of evidence to load the balance. Mark the strong ones — they count double.

  1. On [8, 3, 5, 1], insertion sort needed 6 comparisons; bubble sort needed 12.

    Evidence 1: does it support or challenge the claim?
  2. On the same list, insertion sort usually needs fewer comparisons and moves than bubble sort.

    Evidence 2: does it support or challenge the claim?
  3. On the already sorted list [1, 3, 5, 8], both made 3 comparisons and neither swapped or moved anything.

    Evidence 3: does it support or challenge the claim?
  4. The advantage is 'usually', not 'always'. How much work each does depends on the list.

    Evidence 4: does it support or challenge the claim?
  5. On a long, jumbled list, both become slow.

    Evidence 5: does it support or challenge the claim?

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

1Same sorted result, different amount of work: that's why we compare algorithms.
2Bubble sort: compare neighbours, swap if wrong, repeat passes until a pass has no swaps.
3Insertion sort: take the next item and slide it back into place in the sorted part.
4Insertion sort usually does less work than bubble sort on the same list.
5Nearly sorted list: both do little work. Long, jumbled list: both are slow.

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?
It puts the items of a list into order, for example ascending or descending, numerical or alphabetical.
How can we compare two sorting algorithms that give the same result?
By the work each does (how many comparisons and swaps or moves), how quickly it finishes and how simple it is.
In bubble sort, which items are compared?
Each pair of neighbouring (adjacent) items. If a pair is in the wrong order, they're swapped.
What is a 'pass' in bubble sort?
One trip through the list, comparing each neighbouring pair in turn.
Where is the largest remaining item after one pass of bubble sort?
At the end of the unsorted part. It has 'bubbled' there.
When does bubble sort stop?
When it makes a whole pass with no swaps. That shows the list is sorted.
In insertion sort, what counts as the sorted part at the very start?
Just the first item, on its own.
In insertion sort, what happens to the next item from the unsorted part?
It's inserted into its correct place in the sorted part, with larger items moving along one place to make room.
When does insertion sort stop?
When every item has been inserted, so the whole list is sorted.
What does it mean to trace a sorting algorithm?
Carry it out step by step, writing down the state of the list after each pass or each insertion.
On the same list, which usually does less work: bubble sort or insertion sort?
Insertion sort. It usually needs fewer comparisons and moves, so it's usually quicker.
How much work do the two sorts do on a list that's already, or nearly, in order?
Very little. Insertion sort barely moves anything, and bubble sort can stop early after a pass with no swaps.
Why are both sorts slow on a long, jumbled list?
Because the amount of work grows much faster than the length of the list.
What does 'sorting in place' mean?
Rearranging the items inside the same list, rather than building a new list.
What two things about a list help you choose between bubble sort and insertion sort?
Its size, and how close it already is to being in order.

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 posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.