Algorithms

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

5 in 5

• What is software?
• What is a computer program?
• What is an application?
Algorithms:
What is an algorithm?
Algorithms:
What is an algorithm?

• A step-by-step set of instructions for solving problems. The order in which


you carry out the steps in this process is very important.
• Can you think of any algorithms we may use in the real world?
Who uses algorithms?

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

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

1. Start at the first item in the list.


2. Compare this item with the item to its right.
3. If this item is smaller than the item to its right then leave them; if
not, swap them.
4. Move to the next item in the list
5. Repeat 2 - 4 until you reach the end of the list
6. Go through the list of values for a second time, a third time and so
on, repeating steps 1- 5 until there are no more swaps to be made.
Bubble Sort Example

Start at the first item in the list.

3 1 2 5
Bubble Sort Example

Compare this item with the item to its right.

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

• What is an algorithm? All last lesson

• Complete a bubble sort for the following lists of numbers


20 19 18 17 16
Challenge:
564721
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

1. Compare the second item to the first item


2. If second item in the list is larger than the first item in the list then leave them ,
but if the second item is smaller then swap the two numbers.
3. Move onto the third number. 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.
4. Repeat this process by comparing each number with the numbers to its left
until a smaller number is found. When it is, then insert the number in the correct
position and move the other numbers along.
Insertion sort example

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!

You might also like