Algorithms
Algorithms
• What is software?
• What is a computer program?
• What is an application?
Algorithms:
What is an algorithm?
Algorithms:
What is an algorithm?
• Search engines(Google)
• Game Makers(eg Fifa)
• School timetabling(SIMS)
• Self driving cars
• Apps(Instagram/Tiktok)
• Chefs(eg Recipes)
• Doctors(making a diagnosis)
Algorithm for cleaning teeth
Go to bathroom
Here is a simple algorithm for Pickup toothbrush
cleaning your teeth. We know it Pickup toothpaste
is an algorithm because it is a Put toothpaste on tooth brush
series of steps used to solve a Put toothbrush in mouth
Put toothpaste down
problem.
Clean teeth
Take toothbrush out of mouth
Can this algorithm be improved? Put toothbrush down
Leave bathroom
Algorithm for cleaning teeth
Go to bathroom
One step shown is “Clean teeth”. Pickup toothbrush
Pickup toothpaste
Could we develop our algorithm by
Put toothpaste on tooth brush
including more steps to show what Put toothbrush in mouth
happens when you are actually Put toothpaste down
cleaning your teeth? Clean teeth
What steps do you take when Take toothbrush out of mouth
you are cleaning your teeth? Put toothbrush down
Leave bathroom
Algorithm for cleaning teeth
Go to bathroom Move brush at an angle of 90 degrees (in
Pickup toothbrush relation to the surface of the tooth) to the
bottom right tooth (back molar)
Pickup toothpaste Move the brush in a circular motion against
Put toothpaste on tooth brush the tooth (circles of radius 0.4 cm)
After 3 seconds move the brush one tooth
Put toothbrush in mouth
along towards the centre of the mouth.
Put toothpaste down (repeat this until you reach the bottom left
molar tooth)
Clean teeth
Move brush at an angle of 90 degrees (in
Take toothbrush out of mouth relation to the surface of the tooth) to the top
right tooth (back molar)
Put toothbrush down
Leave bathroom
Your turn
• Write an algorithm for Mr Bersey to throw a ball of paper in the bin. He will
start by the whiteboard with the paper on the desk in front of him.
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
5 in 5
You have been asked to sort the following list into ascending order: 59328417
Start with the second item in the list and compare this item with the first item in
the list.
If the second item in the list is larger than the first item in the list then leave them
in their positions, but if the second item is smaller then swap the two numbers.
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 in the correct position by moving the
other numbers along. 3 5 9 2 8 4 1 7
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 numbers along.
2 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 other numbers along.
2 3 4 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 other numbers along. The list is now
in ascending order
1 2 3 4 5 8 9 7
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
Challenge:
Which algorithm do
18 22 13 15 24 17 19
you think is faster?
Bubble sort or
7 3 1 2 6 4 0 insertion
9 sort?
8 Why 5
• Kahoot!