0% found this document useful (0 votes)
8 views52 pages

L7 Slides - Algorithms - KS4

Lesson 7: Bubble sort

Uploaded by

Kareem Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views52 pages

L7 Slides - Algorithms - KS4

Lesson 7: Bubble sort

Uploaded by

Kareem Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Lesson 7:

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?

Think, write, pair, share.

2
Objectives

Lesson 7: Bubble sort


In this lesson, you will:
● Identify why computers often need to sort data

● Traverse a list of items, swapping the items that are out of order

● Perform a bubble sort to order a list containing sample data

3
Activity 1

Why is sorting important?


Humans like to categorise and order things, to greater or lesser
degrees.

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.

Think about any list of names: they will often be presented


alphabetically to make it easier to find an individual.

4
Activity 1

Why is sorting important?


In computer science, sorting algorithms are used to arrange the items
of a list in a particular order e.g. from lowest to highest.

Computers frequently need to sort large amounts of data based on a


certain attribute.

Can you think of any examples when computers need to sort data?

5
Activity 1

Why is sorting important?


Choosing which sorting algorithm to use depends on certain factors,
such as how efficient it is on large sets of data or how easy it is to
implement and test.

In this lesson, you will explore how to execute an algorithm called


bubble sort.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

Cup 1 Cup 2 Cup 3 Cup 4 Cup 5 Cup 6 Cup 7 Cup 8 Cup 9

43 21 2 50 3 80 35 64 7

● Take a list of data to be sorted.

8
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

43 21 2 50 3 80 35 64 7

current

● Start from the first item in the list.

1
0
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

21 43 2 50 3 80 35 64 7

current

● Go to the next item in the list.

1
4
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

21 2 43 50 3 80 35 64 7

current

● Go to the next item in the list.

1
8
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

21 2 43 50 3 80 35 64 7

current

● Go to the next item in the list.

2
1
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

current

● Go to the next item in the list.

2
5
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

21 2 43 3 50 80 35 64 7

current

● Go to the next item in the list.

2
8
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

21 2 43 3 50 35 80 64 7

current

● Go to the next item in the list.

3
2
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

21 2 43 3 50 35 64 80 7

current

● Go to the next item in the list.

3
6
Activity 2

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

Bubble sort: one pass


Each number is hidden under a cup. The cups need to be sorted with
the lowest value on the left.

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

List after one


pass
21 2 43 3 50 35 64 7 80

● A single pass results in the largest element reaching its final


position at the end of the list, since it will always be greater than
the element it is next to.

● 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

Algorithm for one pass of a bubble sort


The instructions for performing one pass of a bubble sort can be
written as:
Pass
1. Take a list of data to be sorted.
2. Repeat steps a-c for all the items in the list, starting from the first
one:
a. Compare the item at the current position to the one next to it.
b. If the item at the current position is greater than the one next
to it, swap the items within the list.
c. Go to the next item in the list.

4
2
Activity 3

One pass of a bubble sort

You are now going to perform


one pass of a bubble sort on a
list of cards.

Fill in the table on the Activity 3


worksheet to show the order of
the cards after each comparison.

4
3
Activity 3

One pass of a bubble sort - Solution

4
4
Activity 4

Comparing elements with bubble sort


In the last activity, your cards should have been in this order after one
pass:

It should have taken you 7 comparisons to pass over these 8 cards.


The number of comparisons made in a single pass is always equal
to the number of pairs you pass over i.e. the number of elements
- 1.

4
5
Activity 4

Comparing elements with bubble sort


To fully execute a bubble sort, you need to keep passing through the list
until all the elements are in order.

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

Algorithm for a complete bubble sort


The instructions for executing a bubble sort in full can be written as:

I. Take a list of data to be sorted.


II. Repeat step 1 (the pass) until no swaps are made: Pass
1. Repeat steps a-c for all the items in the list, starting from the
first one:
a. Compare the item at the current position to the one next to
it.
b. If the item at the current position is greater than the one
next to it, swap the items within the list.
c. Go to the next item in the list.

4
7
Activity 5

Executing a bubble sort

Complete the tasks on the


Activity 5 worksheet for
executing a bubble sort.

4
8
Activity 5

Executing a bubble sort - Task 1 Solution


Below is the solution for each pass of the bubble sort in the first task:

4
9
Activity 5

Executing a bubble sort


You may have noticed in the last activity that after each pass, the next
largest value is in its proper place.

This means that you would never need to check the largest value again
once a pass has completed.

Therefore, you can reduce the number of comparisons by one


after each pass to improve the efficiency of the algorithm.

5
0
Plenary

Summary of bubble sort


Bubble sort is one of the slowest algorithms for sorting data and
performs poorly in real world use, especially on large collections of data.

However, you can improve the efficiency of the bubble sort algorithm
by:

● Stopping once no swaps were made during a single pass


● Reducing the number of comparisons by 1 after each pass

5
1
Summary

Next lesson

In this lesson, you… Next lesson, you will…

Identified why computers often Explore another sorting


need to sort data. algorithm, insertion sort.

Traversed a list of items,


swapping the items that are out
of order.

Performed a bubble sort to order


a list containing sample data.

5
2

You might also like