Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Bubble sort and insertion sort

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

Sort your hand of cards

You hold five playing cards. You want them in order, from the smallest to the largest. How do you do it?

Some people swap cards that are in the wrong order, again and again. Others take one card at a time and slide it into the right place. These are two real sorting methods. Computers use them too.

Key words

WordWhat it meansExample
Sorting algorithmA set of steps that puts items in order.
Bubble sortCompare neighbours and swap them if they are in the wrong order. Repeat in passes.
Insertion sortTake one item at a time and slide it into its place in the sorted part.
PassOne trip through the list from start to end.not the Python word pass
SwapTwo items change places.
ComparisonChecking one value against another.
In orderSmallest first. This is called ascending order.

The cells students fill in are left blank in this preview.

Bubble sort: swap the neighbours

In a bubble sort you go along the list. Compare each item with the one next to it. If they are in the wrong order, swap them.

The biggest item "bubbles" to the end in the first pass. Do more passes. Stop when a whole pass makes no swaps.

Try it: bubble sort

Code: python
numbers = [5, 3, 8, 1]
swapped = True
pass_number = 0

while swapped:
    swapped = False
    pass_number = pass_number + 1
    for i in range(len(numbers) - 1):
        if numbers[i] > numbers[i + 1]:
            temp = numbers[i]
            numbers[i] = numbers[i + 1]
            numbers[i + 1] = temp
            swapped = True
    print("After pass", pass_number, numbers)

# Try [1, 2, 3, 4]. How many passes now?

Worked example: bubble sort

PassList at the startWhat happensList at the end
15, 3, 8, 15 and 3 swap. 5 and 8 stay. 8 and 1 swap3, 5, 1, 8
23, 5, 1, 83 and 5 stay. 5 and 1 swap. 5 and 8 stay3, 1, 5, 8
33, 1, 5, 83 and 1 swap. The rest stay1, 3, 5, 8
41, 3, 5, 8No swaps at all. The list is sorted: stop1, 3, 5, 8

The cells students fill in are left blank in this preview.

Insertion sort: slide into place

In an insertion sort the left part of the list is always sorted. At the start it is just the first item.

Pick the next item. Slide it left, past every bigger item, until it fits. Now the sorted part is one item longer. Repeat until the end.

Worked example: insertion sort

StepItem pickedWhat happensList after the step
StartThe sorted part is just the 55, 3, 8, 1
135 is bigger, so it moves right. 3 goes in front3, 5, 8, 1
285 is smaller, so 8 stays where it is3, 5, 8, 1
318, 5 and 3 are all bigger and move right. 1 goes at the front1, 3, 5, 8

The cells students fill in are left blank in this preview.

Try it: insertion sort

Code: python
numbers = [5, 3, 8, 1]

for i in range(1, len(numbers)):
    current = numbers[i]
    position = i - 1
    while position >= 0 and numbers[position] > current:
        numbers[position + 1] = numbers[position]
        position = position - 1
    numbers[position + 1] = current
    print("After picking", current, numbers)

# Try [9, 7, 5, 3, 1].

Quick check: how they work

Have a go

Quick check: how they work

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

4 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.