Algorithms 2
Algorithms 2
Challenge:
Write an algorithm for
something you do exactly
the same every day. Be as
specific as you can.
Sorting algorithms
• Sorting algorithms will put items in a list into an order.
• Sorting a list of items can take a long time, but an algorithm will
ensure we can do it in the quickest time possible.
• Why do you think we need an algorithm to do this?
Research task
• Find out what a bubble sort algorithm is and how it works.
• Write down the information in your books
• If you get stuck type into google ‘Bubble sort BBC bitesize ks3’ and
click on the first link
Bubble Sort Algorithm
3 1 2 5
Bubble Sort Example
3 1 2 5
Bubble Sort Example
If this item is smaller than the item to its right leave them;
if not, swap them.
3 1 2 5
Bubble Sort Example
Move to the next item in the list
Compare this item with the item to its right.
If this item is smaller than the next item leave them; if not,
swap them.
1 3 2 5
Bubble Sort Example
Move to the next item in the list
Compare this item with the item to its right.
If this item is smaller than the item to its right leave them; if
not, swap them.
1 2 3 5
Bubble Sort
In this case the list has been sorted. For longer lists you
would need to go through the list of values for a second
time, a third time and so on, you would repeat this until
there were no more swaps to be made.
1 2 3 5
Bubble sort these numbers and letters
•5 3 1 2 4
•1 2 5 3 4
• 10 7 9 6 4
• 13 14 15 16 17
5 in 5
• What is an algorithm?
• Complete a bubble sort for the following lists of numbers
20 19 18 17 16 All last lesson
5 6 4 7 2 1
Challenge:
What is the
minimum amount of
lines it takes to
complete a bubble
sort? Why?
Can we think of any other ways to sort
numbers into an order?
Insertion Sort
The Insertion Sort algorithm is used to sort a list by looking at each
item in turn and inserting this item in the correct position in the
list.
It does this by performing a comparison with the item to its left
and continues doing this until it arrives at its correct position in
the list.
Insertion Sort
5 9 3 2 8 4 1 7
5 9 3 2 8 4 1 7
Insertion sort example
Now examine the third number in the list. If the third item is smaller
than the second item in the list, then compare it with the first item on
the list. If it is smaller than the first item in the list, insert it in this
location by moving the first two numbers along.
5 9 3 2 8 4 1 7
3 5 9 2 8 4 1 7
Insertion sort example
Now examine the fourth number in the list. Compare this number with
the number to its left until a smaller number is found or no smaller
number is found. i.e. compare it with 9 (2 is smaller than 9) and then 5 (2
is smaller than 5) and then 3 (2 is smaller than 3) then insert the number
3 5by9moving
in the correct position 2 8 the
4 other
1 7numbers along.
2 3 5 9 8 4 1 7
Insertion sort example
Now examine the fifth number in the list. Compare this number with the
number to its left until a smaller number is found or no smaller number is
found. i.e. compare it with 9 (8 is smaller than 9) and then 5 (8 is not
smaller than 5) then insert the number in the correct position by moving
the other numbers2 along.
3 5 9 8 4 1 7
2 3 5 8 9 4 1 7
Insertion sort example
Now examine the sixth number in the list. Compare this number with the
number to its left until a smaller number is found or no smaller number is
found. i.e. compare it with 9 (4 is smaller than 9) and then 8 (4 is smaller
than 8) etc. then insert the number in the correct position by moving the
other numbers along.
2 3 5 8 9 4 1 7
2 3 4 5 8 9 1 7
Insertion sort example
Now examine the seventh number in the list. Compare this number with
the number to its left until a smaller number is found or no smaller
number is found. i.e. compare it with 9 (1 is smaller than 9) and then 8 (1
is smaller than 8) etc. then insert the number in the correct position by
moving the other2numbers
3 4 along.
5 8 9 1 7
1 2 3 4 5 8 9 7
Insertion sort example
Now examine the last number in the list. Compare this number with the
number to its left until a smaller number is found or no smaller number is
found. i.e. compare it with 9 (7 is smaller than 9) and then 8 (7 is smaller
than 8) etc. then insert the number in the correct position by moving the
1 2 3 4 5 8 9 7
other numbers along. The list is now in ascending order
1 2 3 4 5 7 8 9
Have a go at solving these by insertion sorts.
Make sure you show every step.
7 8 5 4 2 9
1 5 3 7 9 8 4 2 6 0
18 22 13 15 24 17 19 Challenge:
Which algorithm do
you think is faster?
7 3 1 2 6 4 0 9
Bubble 8 or 5
sort
insertion sort? Why
• Kahoot!
5 in 5
• Perform an insertion sort on the following lists of numbers:
5 8 2 7 4
Last few
13 12 11 14 15
lessons
• Perform a bubble sort on the following lists of numbers:
5 8 2 7 4
13 12 11 14 15
Challenge: If I had more
than 5 numbers would it
take longer to sort my list
(whatever the longer list
is) Why?
Which algorithm do you think is quicker?
Insertion sort or Bubble sort?
Your turn
• Pick any list of 5 numbers e.g. 7 9 3 2 5
• In a PowerPoint please complete an insertion sort and a bubble
sort and put them side by side.
• Research the differences between these sorts – answer the
following questions: Should they both take the same time?
What happens if I have a bigger list?
Is one algorithm more efficient than the
other?
Challenge: Find out about the
quicksort algorithm. Would a
quicksort be a better or worse
algorithm than these two?
Why?
Bubble sort vs Insertion sort
• Both of these algorithms are quick if the list of numbers is small but
the algorithms are not very efficient when sorting a bigger list.
• Both algorithms are easy to code.
• (Bonus) both algorithms take up the same amount of space.
Yr 10 Bubble sort - Details - Kahoot!
Bubble sort quiz - Details - Kahoot!