0% found this document useful (0 votes)
56 views39 pages

T3 Bubble, Merge and Insertion Sort

This document discusses different sorting algorithms: - Bubble sort, insertion sort, and merge sort are introduced as standard sorting algorithms. - Bubble sort is explained through an example of sorting a list of numbers, with each pass allowing the largest value to "bubble" to the end. - Insertion sort is described as sorting items one by one, inserting them into the sorted portion of the list. - An example demonstrates insertion sort by sorting a sample list of numbers from lowest to highest.

Uploaded by

Pixie
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)
56 views39 pages

T3 Bubble, Merge and Insertion Sort

This document discusses different sorting algorithms: - Bubble sort, insertion sort, and merge sort are introduced as standard sorting algorithms. - Bubble sort is explained through an example of sorting a list of numbers, with each pass allowing the largest value to "bubble" to the end. - Insertion sort is described as sorting items one by one, inserting them into the sorted portion of the list. - An example demonstrates insertion sort by sorting a sample list of numbers from lowest to highest.

Uploaded by

Pixie
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/ 39

Objectives

• Understand the standard sort algorithms:


• Bubble sort
• Insertion sort
• Merge sort

• Be able to apply each algorithm to a data set


• Be able to Identify an algorithm if given the
code for it
Sorting algorithms
Unit 6 Algorithms

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

Source: ONS (2019)


Sorting algorithms
Unit 6 Algorithms

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

• The girls and boys data sets could be combined to


show the top 20 names in England and Wales
• Again these could be sorted by count or name
Sorting algorithms
Unit 6 Algorithms

The bubble sort


• Start with the leftmost item
• Compare this item with the one next to it
• If the one next to it is less, swap the items
• Repeat for all the other items
• At the end of one pass through the list, the largest item is at
the end of the list
• Repeat the process until the items are sorted
• Suppose you have a list of numbers to be sorted:

9 5 4 15 3 8 11 2
Sorting algorithms
Unit 6 Algorithms

Bubble sort – First pass


9 5 4 15 3 8 11 2 • Each item is compared
5 9 4 15 3 8 11 2 with the one on its right,
and swapped if it is
5 4 9 15 3 8 11 2
larger
5 4 9 15 3 8 11 2
• At the end of the first
5 4 9 3 15 8 11 2 pass the largest item
5 4 9 3 8 15 11 2 bubbles through to the
end of the list
5 4 9 3 8 11 15 2
• (Mauve indicates sorted
5 4 9 3 8 11 2 15
items)
Sorting algorithms
Unit 6 Algorithms

First pass Second pass


9 5 4 15 3 8 11 2 5 4 9 3 8 11 2 15

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

Second pass Third pass


5 4 9 3 8 11 2 15 4 5 3 8 9 2 11 15

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

Third pass Fourth pass


4 5 3 8 9 2 11 15 3 4 5 8 2 9 11 15

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

Fourth pass Fifth pass


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 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

Fifth pass Sixth pass


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 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

Sixth pass Seventh pass


3 4 2 5 8 9 11 15 3 2 4 5 8 9 11 15

3 4 2 5 8 9 11 15 2 3 4 5 8 9 11 15

3 2 4 5 8 9 11 15 Seven passes through the list of eight


numbers ensures that they are sorted
Sorting algorithms
Unit 6 Algorithms

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

Leave the first item at the start: 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

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 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

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 4 15 3 8 11 2

4 is now inserted into the sorted list: 4 5 9 15 3 8 11 2


Sorting algorithms
Unit 6 Algorithms

Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 4 15 3 8 11 2

4 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

15 is now inserted into the sorted list: 4 5 9 15 3 8 11 2


Sorting algorithms
Unit 6 Algorithms

Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 4 15 3 8 11 2

4 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

15 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

3 is now inserted into the sorted list: 3 4 5 9 15 8 11 2


Sorting algorithms
Unit 6 Algorithms

Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 4 15 3 8 11 2

4 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

15 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

3 is now inserted into the sorted list: 3 4 5 9 15 8 11 2

8 is now inserted into the sorted list: 3 4 5 8 9 15 11 2


Sorting algorithms
Unit 6 Algorithms

Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 4 15 3 8 11 2

4 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

15 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

3 is now inserted into the sorted list: 3 4 5 9 15 8 11 2

8 is now inserted into the sorted list: 3 4 5 8 9 15 11 2

11 is now inserted into the sorted list: 3 4 5 8 9 11 15 2


Sorting algorithms
Unit 6 Algorithms

Insertion sort
List to be sorted: 9 5 4 15 3 8 11 2

Leave the first item at the start: 9 5 4 15 3 8 11 2

5 is now inserted into the sorted list: 5 9 4 15 3 8 11 2

4 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

15 is now inserted into the sorted list: 4 5 9 15 3 8 11 2

3 is now inserted into the sorted list: 3 4 5 9 15 8 11 2

8 is now inserted into the sorted list: 3 4 5 8 9 15 11 2

11 is now inserted into the sorted list: 3 4 5 8 9 11 15 2

2 is now inserted into the sorted list: 2 3 4 5 8 9 11 15

Which item did not have to move?


Sorting algorithms
Unit 6 Algorithms

Worksheet 3
• Now complete Task 2 on Worksheet 3
Sorting algorithms
Unit 6 Algorithms

Merging two lists


List A List B

6 8 13 20 2 7 11 16

• The following slides show how to merge two sorted


lists together
• Read item from list A; Read item from list B.
• Write smaller to output list.
• Read next item from the list that held the smaller value
• Repeat until all items written to output list
Sorting algorithms
Unit 6 Algorithms

Merging two lists


List A List B

6 8 13 20 2 7 11 16

• Read item from list A 6 Compare and write


smaller item to output
• Read item from list B 2
Output list

2
Sorting algorithms
Unit 6 Algorithms

Merging two lists


List A List B

6 8 13 20 2 7 11 16

• Read item from list A 6 Compare and write


smaller item to output
• Read item from list B 7
Output list

2 6
Sorting algorithms
Unit 6 Algorithms

Merging two lists


List A List B

6 8 13 20 2 7 11 16

• Read item from list A 8 Compare and write


smaller item to output
• Read item from list B 7
Output list

2 6 7
Sorting algorithms
Unit 6 Algorithms

Merging two lists


List A List B

6 8 13 20 2 7 11 16

• Read item from list A 8 Compare and write


smaller item to output
• Read item from list B 11
Output list

2 6 7 8
Sorting algorithms
Unit 6 Algorithms

Merging two lists


List A List B

6 8 13 20 2 7 11 16

• Continue with the rest of the comparisons

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

Merge sort part 1


• Divide the unsorted list into n sublists
Sorting algorithms
Unit 6 Algorithms

Merge sort part 2


• Now merge the pairs of sub-lists into a single
sorted list
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

• Which of these algorithms would you


generally choose? Why?
Sorting algorithms
Unit 6 Algorithms

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

© 2020 PG Online Limited

The contents of this unit are protected by 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.

You might also like