L7 Slides - Algorithms - KS4
L7 Slides - Algorithms - KS4
Bubble sort
KS4 - Algorithms
Starter activity
Finding a surname
List Jones Davie Wilson Khan Taylor James Smith Ali Brown Patel
1: s
List Ali Brown Davie James Jones Khan Patel Smith Taylor Wilson
2: s
Given the two lists above, which list do you think it would be easier to
find a surname in? Why do you think that?
2
Objectives
● Traverse a list of items, swapping the items that are out of order
3
Activity 1
In supermarkets, the fresh vegetables and fruit will have been placed
together, bakery goods somewhere else in the shop, tinned goods in
Sorting
anotherisaisle.
usually done to make searching
faster.
4
Activity 1
Can you think of any examples when computers need to sort data?
5
Activity 1
6
Activity 2
Bubble sort
The bubble sort algorithm works by repeatedly going through a list,
comparing adjacent items and swapping the items if they are in the
wrong order.
Each time the algorithm goes through the list it is called a pass. The
pass through the list is repeated until the list is sorted.
In the next slides, you will see how the algorithm swaps adjacent items
that are in the wrong order during one pass of a bubble sort.
7
Activity 2
43 21 2 50 3 80 35 64 7
8
Activity 2
43 21 2 50 3 80 35 64 7
Here is the initial order of the cups. However, when you are executing a
bubble sort you will only be comparing two items at a time.
9
Activity 2
43 21 2 50 3 80 35 64 7
current
1
0
Activity 2
43 21 2 50 3 80 35 64 7
current
● Compare the item at the current position to the one next to it.
1
1
Activity 2
Cup 1 Cup 2
43 21 2 50 3 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
1
2
Activity 2
Cup 1 Cup 2
21 43 2 50 3 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
1
3
Activity 2
21 43 2 50 3 80 35 64 7
current
1
4
Activity 2
21 43 2 50 3 80 35 64 7
current
● Compare the item at the current position to the one next to it.
1
5
Activity 2
21 43 2 50 3 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
1
6
Activity 2
21 2 43 50 3 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
1
7
Activity 2
21 2 43 50 3 80 35 64 7
current
1
8
Activity 2
21 2 43 50 3 80 35 64 7
current
● Compare the item at the current position to the one next to it.
1
9
Activity 2
21 2 43 50 3 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
2
0
Activity 2
21 2 43 50 3 80 35 64 7
current
2
1
Activity 2
21 2 43 50 3 80 35 64 7
current
● Compare the item at the current position to the one next to it.
2
2
Activity 2
21 2 43 50 3 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
2
3
Activity 2
21 2 43 3 50 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
2
4
Activity 2
current
2
5
Activity 2
21 2 43 3 50 80 35 64 7
current
● Compare the item at the current position to the one next to it.
2
6
Activity 2
21 2 43 3 50 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
2
7
Activity 2
21 2 43 3 50 80 35 64 7
current
2
8
Activity 2
21 2 43 3 50 80 35 64 7
current
● Compare the item at the current position to the one next to it.
2
9
Activity 2
21 2 43 3 50 80 35 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
3
0
Activity 2
21 2 43 3 50 35 80 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
3
1
Activity 2
21 2 43 3 50 35 80 64 7
current
3
2
Activity 2
21 2 43 3 50 35 80 64 7
current
● Compare the item at the current position to the one next to it.
3
3
Activity 2
21 2 43 3 50 35 80 64 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
3
4
Activity 2
21 2 43 3 50 35 64 80 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
3
5
Activity 2
21 2 43 3 50 35 64 80 7
current
3
6
Activity 2
21 2 43 3 50 35 64 80 7
current
● Compare the item at the current position to the one next to it.
3
7
Activity 2
21 2 43 3 50 35 64 80 7
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
3
8
Activity 2
21 2 43 3 50 35 64 7 80
current
● If the item at the current position is greater than the one next to
it,
swap the items within the list.
3
9
Activity 2
21 2 43 3 50 35 64 7 80
● One pass has now been completed. If any swaps were made in this
pass then keep passing through the list until no swaps are made.
4
0
Activity 2
Bubble sort
Original
43 list 21 2 50 3 80 35 64 7
● This means that the next pass doesn’t need to check the final pair
of elements as you know the largest element is in the right place.
4
1
Activity 2
4
2
Activity 3
4
3
Activity 3
4
4
Activity 4
4
5
Activity 4
How do you know that the list has been sorted after you’ve finished a
pass?
If no swaps were made during a single pass, that means that
none of the elements are out of order and the algorithm can stop
executing.
4
6
Activity 4
4
7
Activity 5
4
8
Activity 5
4
9
Activity 5
This means that you would never need to check the largest value again
once a pass has completed.
5
0
Plenary
However, you can improve the efficiency of the bubble sort algorithm
by:
5
1
Summary
Next lesson
5
2