L8 Slides - Algorithms - KS4
L8 Slides - Algorithms - KS4
Insertion sort
KS4 - Algorithms
Starter activity
2
Objectives
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.
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:
5
Activity 1
Inserting an item
6
Activity 1
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
2 21 43 80
sorted unsorted
● Take a list where the first items are ordered and the rest are not.
9
Activity 2
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
2 21 43 50 35 35
value
2 21 43 50 35 35
current value
1
2
Activity 2
2 21 43 80 35 35
current value
2 21 43 80 80 35
current value
2 21 43 50 50 35
current value
2 21 43 50 50 35
current value
2 21 43 43 50 35
current value
2 21 43 43 50 35
current value
2 21 43 43 50 35
current value
1
9
Activity 2
2 21 35 43 50 35
current value
2
0
Activity 2
2 21 35 43 80 35
● One pass has now been completed, increasing the sorted sublist by
one item.
2
1
Activity 2
2
3
Activity 3
2
4
Activity 3
sorted unsorted
● Let the unsorted sublist contain all the items, except the first one.
2
5
Activity 3
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
43 21
43 21
21 43
21 43
sorted unsorted
● One pass has now been completed, increasing the sorted sublist by
one item.
3
0
Activity 3
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
21 43 2
21 43 2
2 21 43
2 21 43
sorted unsorted
● One pass has now been completed, increasing the sorted sublist by
one item.
3
5
Activity 3
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
2 21 43 80
2 21 43 80
2 21 43 80
2 21 43 80
sorted unsorted
● One pass has now been completed, increasing the sorted sublist by
one item.
4
0
Activity 3
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
2 21 43 80 3
2 21 43 80 3
2 3 21 43 80
2 3 21 43 80
sorted unsorted
● One pass has now been completed, increasing the sorted sublist by
one item.
4
5
Activity 3
2 3 21 43 80 35
sorted unsorted
4
6
Activity 3
2 3 21 43 80 35
2 3 21 43 80 35
2 3 21 35 43 80
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
5
2
Activity 4
5
3
Activity 5
5
4
Plenary
5
5
Homework
5
6
Summary
Next lesson
5
7