T3 Bubble, Merge and Insertion Sort
T3 Bubble, Merge and Insertion Sort
Starter
• Both people and computers often need data
to be sorted
• Give five types of data that people need to be sorted
• Give one reason why a computer would work more efficiently
if a list was sorted
Sorting algorithms
Unit 6 Algorithms
Starter
• Types of data that people need sorted
• Index cards/records of customer details
• Directories and dictionaries
• House numbers
• Library books
• Stock in a warehouse
• and many more…
• Reasons why a computer will work more efficiently
with sorted lists
• Computers can use a binary search with sorted lists which is
far more efficient than a linear search
Sorting algorithms
Unit 6 Algorithms
Sorting
• Data sets frequently need to be sorted
Rank Name Count Rank Name Count
• Look at the two
data sets of the 1 Oliver 5 390 1 Olivia 4 598
top 10 boys and 2 George 4 960 2 Amelia 3 941
girls names 3 Harry 4 512 3 Ava 3 110
in 2019 4 Noah 4 107 4 Isla 3 046
• What other orders 5 Jack 3 988 5 Emily 2 676
could they 6 Leo 3 721 6 Mia 2 490
be sorted in?
7 Arthur 3 644 7 Isabella 2 369
• Why might you 8 Muhammad 3 507 8 Sophia 2 344
need to sort the 9 Oscar 3 459 9 Ella 2 326
data sets?
10 Charlie 3 365 10 Grace 2 301
Sorting
• The data sets could be sorted as follows:
• Count order (which will be the same as the rank order)
• Alphabetical order
• Reverse alphabetical or count order
9 5 4 15 3 8 11 2
Sorting algorithms
Unit 6 Algorithms
5 9 4 15 3 8 11 2 4 5 9 3 8 11 2 15
5 4 9 15 3 8 11 2 4 5 9 3 8 11 2 15
5 4 9 15 3 8 11 2 4 5 3 9 8 11 2 15
5 4 9 3 15 8 11 2 4 5 3 8 9 11 2 15
5 4 9 3 8 15 11 2 4 5 3 8 9 11 2 15
5 4 9 3 8 11 15 2 4 5 3 8 9 2 11 15
5 4 9 3 8 11 2 15
Sorting algorithms
Unit 6 Algorithms
4 5 9 3 8 11 2 15 4 5 3 8 9 2 11 15
4 5 9 3 8 11 2 15 4 3 5 8 9 2 11 15
4 5 3 9 8 11 2 15 4 3 5 8 9 2 11 15
4 5 3 8 9 11 2 15 4 3 5 8 9 2 11 15
4 5 3 8 9 11 2 15 4 3 5 8 2 9 11 15
4 5 3 8 9 2 11 15
Sorting algorithms
Unit 6 Algorithms
4 5 3 8 9 2 11 15 3 4 5 8 2 9 11 15
4 3 5 8 9 2 11 15 3 4 5 8 2 9 11 15
4 3 5 8 9 2 11 15 3 4 5 8 2 9 11 15
4 3 5 8 9 2 11 15 3 4 5 2 8 9 11 15
4 3 5 8 2 9 11 15
Sorting algorithms
Unit 6 Algorithms
3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15
3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15
3 4 5 8 2 9 11 15 3 4 2 5 8 9 11 15
3 4 5 2 8 9 11 15
Sorting algorithms
Unit 6 Algorithms
3 4 5 2 8 9 11 15 3 4 2 5 8 9 11 15
3 4 5 2 8 9 11 15 3 2 4 5 8 9 11 15
3 4 2 5 8 9 11 15
3 4 2 5 8 9 11 15 2 3 4 5 8 9 11 15
Worksheet 3
• Now complete Task 1 on Worksheet 3
Sorting algorithms
Unit 6 Algorithms
Insertion sort
• This algorithm sorts one data item at a time
• It is similar to how you might sort a deck of cards
• One item is taken from the list, and placed in the
correct position
• This is repeated until there are no more unsorted items in
the list
Sorting algorithms
Unit 6 Algorithms
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Sorting algorithms
Unit 6 Algorithms
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2
Worksheet 3
• Now complete Task 2 on Worksheet 3
Sorting algorithms
Unit 6 Algorithms
6 8 13 20 2 7 11 16
6 8 13 20 2 7 11 16
2
Sorting algorithms
Unit 6 Algorithms
6 8 13 20 2 7 11 16
2 6
Sorting algorithms
Unit 6 Algorithms
6 8 13 20 2 7 11 16
2 6 7
Sorting algorithms
Unit 6 Algorithms
6 8 13 20 2 7 11 16
2 6 7 8
Sorting algorithms
Unit 6 Algorithms
6 8 13 20 2 7 11 16
Output list
2 6 7 8 11 13 16 20
Sorting algorithms
Unit 6 Algorithms
Merge sort
• This is much more efficient than the bubble sort
• The basic steps are:
• Divide the unsorted list in two
• Continue to divide these lists into two until there is
just one item in each list
• Now merge each list back until there is only one
list remaining – which will be the fully sorted list
• Perform a merge sort on the following list:
8 13 6 20 11 2 7 16
Sorting algorithms
Unit 6 Algorithms
Worksheet 3
• Now complete Task 3, Task 4 and Task 5 on
Worksheet 3
Sorting algorithms
Unit 6 Algorithms
Comparing sorts
• The Insertion sort is much quicker than the Bubble
sort, but the Merge sort is faster still
• Be sure you can explain how each sort works
Sorting algorithms
Unit 6 Algorithms
Plenary
• Name three algorithms which can be
used to sort lists of data
• Explain briefly how each one works
Plenary
• Sorting algorithms
• Bubble sort
• Insertion sort
• Merge sort
• Choice of algorithm
• Merge sort is generally faster to sort lists,
so would be the recommended algorithm
Sorting algorithms
Unit 6 Algorithms
Copyright
This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.
Licence agreement
This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.
The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.