0% found this document useful (0 votes)
20 views57 pages

L8 Slides - Algorithms - KS4

Lesson 8: Insertion 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)
20 views57 pages

L8 Slides - Algorithms - KS4

Lesson 8: Insertion 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/ 57

Lesson 8:

Insertion sort
KS4 - Algorithms
Starter activity

Where does this book go?


Given a list of books with the author names as above, where would you
insert an additional book by George Orwell?

Enid Agatha Roald Patrick J.K. J. R. R. Akira Virgina


Blyton Christie Dahl Rothfuss Rowling Tolkien Toriyama Woolf

Think, write, pair, share.

2
Objectives

Lesson 8: Insertion sort


In this lesson, you will:
● Insert an item into an ordered list of items

● Describe how insertion sort is used for ordering a list of items

● Perform an insertion sort to order a list containing sample data

3
Activity 1

Inserting an item
In the previous activity, you probably wouldn’t have added the new
book to a random place in the list of books and then used a bubble sort
to order the list.
Georg
Enid Agatha Roald Patrick J.K. J. R. R. Akira Virgina
e
Blyton Christie Dahl Rothfuss Rowling Tolkien Toriyama Woolf
Orwell

It would make more sense to make space for the new book and insert it
into the correct position in the list.

This is essentially how an insertion sort works.


4
Activity 1

Inserting an item
Imagine you are sorting a pack of cards into order. To add a card into
the sorted list of cards you would probably do something like:

1. Retrieve a card from the unsorted pile.


2. Make room for the card in the sorted list.
3. Insert the card into the correct position in the list.

5
Activity 1

Inserting an item

Using the instructions in the


previous slide, you are going to
insert a card into a sorted list of
cards.

Fill in the table on the Activity 1


worksheet to show the list of
cards after one insertion.

6
Activity 1

Inserting an item - Solution

7
Activity 2

Insertion sort
The insertion sort algorithm works by grouping the items in a list into
two parts: a sorted sublist and an unsorted sublist.

With each pass through the list, an item from the unsorted sublist is
compared to items in the sorted sublist until it is inserted into the
correct position.

In the next slides, you will first see how the algorithm inserts one out-of-
order item into the correct position in the list i.e. one pass of the
algorithm.

8
Activity 2

Insertion sort: single 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

2 21 43 80

sorted unsorted

● Take a list where the first items are ordered and the rest are not.

9
Activity 2

Insertion sort: single 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

2 21 43 80

sorted unsorted

● Take a list where the first items are ordered and the rest are not.
In each single pass, the first out-of-order item is inserted at the appropriate position in
the sorted list.
1
0
Activity 2

Insertion sort: single 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

2 21 43 50 35 35

value

● Copy the first out-of-order item into value.


Taking a copy of the last, out-of-order item is necessary. As items are “shifted” to the
right, to make room for the new value, the last item in the list will be overwritten.
1
1
Activity 2

Insertion sort: single 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

2 21 43 50 35 35

current value

● Start with the last of the ordered items.

1
2
Activity 2

Insertion sort: single 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

2 21 43 80 35 35

current value

● If the item at the current position is greater than value,


copy it into the next item.
Move on to the previous item in the list.
1
3
Activity 2

Insertion sort: single 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 5 Cup 6 Cup 7

2 21 43 80 80 35

current value

● If the item at the current position is greater than value,


copy it into the next item.
Move on to the previous item in the list.
1
4
Activity 2

Insertion sort: single 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

2 21 43 50 50 35

current value

● If the item at the current position is greater than value,


copy it into the next item.
Move on to the previous item in the list.
1
5
Activity 2

Insertion sort: single 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

2 21 43 50 50 35

current value

● If the item at the current position is greater than value,


copy it into the next item.
Move on to the previous item in the list.
1
6
Activity 2

Insertion sort: single 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

2 21 43 43 50 35

current value

● If the item at the current position is greater than value,


copy it into the next item.
Move on to the previous item in the list.
1
7
Activity 2

Insertion sort: single 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

2 21 43 43 50 35

current value

● If the item at the current position is greater than value,


copy it into the next item.
Move on to the previous item in the list.
1
8
Activity 2

Insertion sort: single 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

2 21 43 43 50 35

current value

● If the item at the current position is not greater than value,


value is ready to be inserted in the list.

1
9
Activity 2

Insertion sort: single 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

2 21 35 43 50 35

current value

● Copy value into the item after the current position.

2
0
Activity 2

Insertion sort: single 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

2 21 35 43 80 35

sorted unsorted value

● One pass has now been completed, increasing the sorted sublist by
one item.

2
1
Activity 2

Algorithm for one pass of insertion sort


The instructions for executing one pass of insertion sort can be written
as:
Pass
1. Take a list where the first items are ordered and the rest are not.
2. Copy the first out-of-order item into value.
3. Repeat steps a-b, starting from the last of the ordered items,
until no more items remain or value is ready to be inserted in the
list:
a. If the item at the current position is greater than value,
copy it into the next item. Move on to the previous item in the
list.
b. Otherwise, value is ready to be inserted in the list.
4. Copy value into the item after the current position. 2
2
Activity 3

Insertion sort: multiple passes


Now that you have seen how one pass of an insertion sort works, the
next slides will go through multiple passes to show you how a
complete insertion sort is executed.

2
3
Activity 3

Insertion sort: multiple passes


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

● Take a list of data to be sorted.

2
4
Activity 3

Insertion sort: multiple passes


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

sorted unsorted

● Let the unsorted sublist contain all the items, except the first one.

2
5
Activity 3

Insertion sort: multiple passes


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

43 21

sorted unsorted

● In the first pass, the first out-of-order item will be inserted at the
appropriate position in the sorted list.

2
6
Activity 3

Insertion sort: multiple passes


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

Cup 1 Cup 3 Cup 4 Cup 5 Cup 6

43 21

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 2
7
Activity 3

Insertion sort: multiple passes


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

Cup 1 Cup 3 Cup 4 Cup 5 Cup 6

43 21

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 2
8
Activity 3

Insertion sort: multiple passes


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

Cup 3 Cup 4 Cup 5 Cup 6

21 43

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 2
9
Activity 3

Insertion sort: multiple passes


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

Cup 3 Cup 4 Cup 5 Cup 6

21 43

sorted unsorted

● One pass has now been completed, increasing the sorted sublist by
one item.

3
0
Activity 3

Insertion sort: multiple passes


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

21 43 2

sorted unsorted

● In the next pass, the first out-of-order item will be inserted at the
appropriate position in the sorted list.

3
1
Activity 3

Insertion sort: multiple passes


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

21 43 2

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 3
2
Activity 3

Insertion sort: multiple passes


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

21 43 2

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 3
3
Activity 3

Insertion sort: multiple passes


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

2 21 43

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 3
4
Activity 3

Insertion sort: multiple passes


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

2 21 43

sorted unsorted

● One pass has now been completed, increasing the sorted sublist by
one item.

3
5
Activity 3

Insertion sort: multiple passes


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

2 21 43 50

sorted unsorted

● In the next pass, the first out-of-order item will be inserted at the
appropriate position in the sorted list.

3
6
Activity 3

Insertion sort: multiple passes


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

2 21 43 80

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 3
7
Activity 3

Insertion sort: multiple passes


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

2 21 43 80

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 3
8
Activity 3

Insertion sort: multiple passes


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

2 21 43 80

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 3
9
Activity 3

Insertion sort: multiple passes


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

2 21 43 80

sorted unsorted

● One pass has now been completed, increasing the sorted sublist by
one item.

4
0
Activity 3

Insertion sort: multiple passes


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

2 21 43 80 3

sorted unsorted

● In the next pass, the first out-of-order item will be inserted at the
appropriate position in the sorted list.

4
1
Activity 3

Insertion sort: multiple passes


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

2 21 43 80 3

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 4
2
Activity 3

Insertion sort: multiple passes


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

2 21 43 80 3

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 4
3
Activity 3

Insertion sort: multiple passes


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

2 3 21 43 80

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 4
4
Activity 3

Insertion sort: multiple passes


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

2 3 21 43 80

sorted unsorted

● One pass has now been completed, increasing the sorted sublist by
one item.

4
5
Activity 3

Insertion sort: multiple passes


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

2 3 21 43 80 35

sorted unsorted

● In the next pass, the last remaining out-of-order item will be


inserted at the appropriate position in the sorted list.

4
6
Activity 3

Insertion sort: multiple passes


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

2 3 21 43 80 35

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 4
7
Activity 3

Insertion sort: multiple passes


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

2 3 21 43 80 35

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 4
8
Activity 3

Insertion sort: multiple passes


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

2 3 21 35 43 80

● Retrieve the first out-of-order item,


compare it to the items in the ordered list and
“shift” these items until the new item is inserted into the correct
position. 4
9
Activity 3

Insertion sort: multiple passes


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

2 3 21 35 43 80

sorted

● One pass has now been completed and the list of items is now
ordered.

5
0
Activity 3

Algorithm for a complete insertion sort


The instructions for executing an insertion sort in full can be written as:

I. Take a list of data to be sorted.


II. Let the unsorted sublist contain all the items, except the first one.
III. Repeat steps 1-3 (the pass) until the unsorted list is empty: Pass
1. Copy the first out-of-order item into value.
2. Repeat steps a-b, starting from the last of the ordered items,
until no more items remain or value is ready to be inserted in
the list:
a. If the item at the current position is greater than value,
copy it into the next item. Move on to the previous item in
the list.
b. Otherwise, value is ready to be inserted in the list. 5
1
Activity 4

Executing an insertion sort

Complete the tasks on the


Activity 4 worksheet for
executing an insertion sort.

5
2
Activity 4

Executing an insertion sort


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

5
3
Activity 5

Comparing insertion sort to bubble sort


You may have noticed that an insertion sort passes over the list a
similar amount of times to bubble sort.

However, an insertion sort usually involves less comparisons per


pass.

This means that insertion sort is generally quicker to execute than


bubble sort, especially on large data sets.

5
4
Plenary

Summary of insertion sort


● An insertion sort groups the items of a list into two parts: a sorted
sublist and an unsorted sublist.
● The algorithm takes each item from the unsorted sublist, compares
it to the items in the sorted sublist and places it in the correct
position.
● People often perform an insertion sort when they are putting objects
into order, like books or cards.

5
5
Homework

Homework: Describing insertion sort

In your own words, write the


instructions for carrying out an
insertion sort on a pack of cards.

Anyone should be able to follow


these steps without needing to
know about insertion sort
beforehand.

Due: Next lesson

5
6
Summary

Next lesson

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

Inserted an item into an ordered Analyse and interpret Python


list of items. code for bubble sort and insertion
sort.
Described how insertion sort is
used for ordering a list of items.

Performed an insertion sort to


order a list containing sample
data.

5
7

You might also like