0% found this document useful (0 votes)
20 views

Algorithms 2

The document provides information about algorithms and different sorting algorithms. It defines an algorithm as a step-by-step set of instructions to solve a problem. Examples of algorithms in real life include search engines, apps, recipes, and more. The document then explains bubble sort and insertion sort algorithms. Bubble sort works by comparing adjacent items and swapping them if out of order, while insertion sort inserts each item into the correctly sorted position. Examples are provided to demonstrate how each algorithm sorts a list of numbers.

Uploaded by

jbersey15
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Algorithms 2

The document provides information about algorithms and different sorting algorithms. It defines an algorithm as a step-by-step set of instructions to solve a problem. Examples of algorithms in real life include search engines, apps, recipes, and more. The document then explains bubble sort and insertion sort algorithms. Bubble sort works by comparing adjacent items and swapping them if out of order, while insertion sort inserts each item into the correctly sorted position. Examples are provided to demonstrate how each algorithm sorts a list of numbers.

Uploaded by

jbersey15
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

5 in 5

• What is a network? All this term


• What is a LAN?
• What is software?
• What is a computer program?
• What is an application?
Challenge: How do you
think computer programs
and applications are made?
Be as specific as you can.
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

Here is a simple algorithm for Go to bathroom


cleaning your teeth. We know it Pickup toothbrush
is an algorithm because it is a Pickup toothpaste
series of steps used to solve a Put toothpaste on tooth brush
problem. Put toothbrush in mouth
Put toothpaste down
Clean teeth
Can this algorithm be improved? Take toothbrush out of mouth
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
Put toothpaste on tooth brush
by including more steps to show
Put toothbrush in mouth
what happens when you are
Put toothpaste down
actually 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 at the back corner of the classroom with the paper on his
desk.

• Be as specific as you can.

• If you finish go through the algorithm in your head – could anything


go wrong at any point? What could you change to avoid that
happening?
Your turn
• Log onto the computer
• Take note of each step as you do.
• Once you have logged on, open a word document and write an
algorithm for logging onto the computer.
5 in 5
• What is an algorithm?
• What can algorithms be used for?
• Give 3 examples of algorithms in real life. All last lesson

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

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

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

You might also like