0% found this document useful (0 votes)
11 views27 pages

T4 Standard Algorithms

The document outlines standard algorithms for problem-solving, including linear search, bubble sort, counting, and finding maximum, minimum, and average values. It provides examples of algorithms for counting sushi on a conveyor belt and finding the most expensive sushi, as well as details on how to implement a bubble sort. Additionally, it discusses the importance of sorting data for efficiency in computer operations.

Uploaded by

minh024055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views27 pages

T4 Standard Algorithms

The document outlines standard algorithms for problem-solving, including linear search, bubble sort, counting, and finding maximum, minimum, and average values. It provides examples of algorithms for counting sushi on a conveyor belt and finding the most expensive sushi, as well as details on how to implement a bubble sort. Additionally, it discusses the importance of sorting data for efficiency in computer operations.

Uploaded by

minh024055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Objectives

• Understand standard methods of solution including:


• Linear search
• Bubble sort
• Totalling
• Counting
• Finding maximum, minimum and average values

• Explain the purpose of a given algorithm including:


• Stating the purpose of an algorithm
• Describing the processes involved in an algorithm
Standard Algorithms
Unit 8 Algorithm design and problem solving

Starter
• A sushi restaurant carries sushi to customers via a
conveyor belt
• An infra-red sensor detects if a sushi leaves the kitchen
• Each item also has a price detected by a barcode scanner
• What is an algorithm to count the number of sushi that
have passed?
• What is an algorithm to find the most expensive sushi?
Standard Algorithms
Unit 8 Algorithm design and problem solving

Starter
Counting sushi Most expensive sushi
Count  0 Max  0
REPEAT REPEAT
IF SushiDetected IF SushiDetected
THEN THEN
Count  Count + 1 INPUT Price
ENDIF IF Price > Max
UNTIL ConveyorStops THEN
Max  Price
ENDIF
ENDIF
UNTIL ConveyorStops
Standard Algorithms
Unit 8 Algorithm design and problem solving

Counting
• Counting is one of the simplest algorithms
to understand
• What does the following algorithm do?
1 FOR i  1 TO 10
2 OUTPUT i
3 NEXT i

• How could you count in twos (even numbers only)?


Standard Algorithms
Unit 8 Algorithm design and problem solving

Counting
• What does the following algorithm do?
1 FOR i  1 TO 10
2 OUTPUT I
3 NEXT i
The algorithm will output: 1 2 3 4 5 6 7 8 9 10
• How could you count in twos (even numbers only)?
1 FOR i  2 TO 10 STEP 2
2 OUTPUT I
3 NEXT i
This outputs: 2 4 6 8 10
• Notice that when we count, we need a variable to store the
counter in. In this case, i is used (it’s known as the iterator)
• Compare this algorithm to the counting algorithm that was
used as the starter answer
Standard Algorithms
Unit 8 Algorithm design and problem solving

Totalling, minimum and average


• For the sushi conveyor, how could you find the
following for all sushi?
• The total cost of all sushi on The algorithm to count the
number of sushi was:
the conveyor
Count  0
• The average price of the items REPEAT
on the conveyor IF SushiDetected
THEN
• The cheapest item on the conveyor Count  Count +
1
ENDIF
UNTIL ConveyorStops
Standard Algorithms
Unit 8 Algorithm design and problem solving

Totalling, minimum and average


Total Average Cheapest
Total  0 Total  0 Min  0
WHILE SushiDetected Count  0 First  TRUE
INPUT Price WHILE SushiDetected WHILE SushiDetected
Total  Total INPUT Price INPUT Price
+ Price Total  Total IF First
ENDWHILE + Price THEN
Count  Count Min  Price
+ 1 ELSE
ENDWHILE First  FALSE
Average  Total / IF Price < Min
Count THEN
Min  Price
ENDIF
ENDIF
ENDWHILE
Standard Algorithms
Unit 8 Algorithm design and problem solving

Worksheet 4
• Complete Task 1 and Task 2 on Worksheet 4
Standard Algorithms
Unit 8 Algorithm design and problem solving

Linear search
• If the list to be searched is not sorted, it is not
possible to do a binary search
• A linear search may be carried out instead

145 27 83 777 492 588 91 678 399 123

• Items are examined in the sequence


145, 27, 83, 777….
Standard Algorithms
Unit 8 Algorithm design and problem solving

A search algorithm
1 Numbers  [5,1,9,8,7,6,4,10]
2 USERINPUT SearchItem
3 FOR i  1 TO LENGTH(Numbers)
4 IF Numbers[i]  SearchItem
5 THEN
6 OUTPUT "SearchItem found"
7 ENDIF
8 ENDFOR
• The above algorithm asks the user to input a number
and then searches for it in the array numbers
• What is the value of LENGTH(numbers) – 1 ?
• When will "SearchItem found" be output?
• Name the type of search algorithm
Standard Algorithms
Unit 8 Algorithm design and problem solving

A search algorithm
1 Numbers  [5,1,9,8,7,6,4,10]
2 USERINPUT SearchItem
3 FOR i  1 TO LENGTH(Numbers)
4 IF Numbers[i]  SearchItem
5 THEN
6 OUTPUT "SearchItem found"
7 ENDIF
8 ENDFOR
• The above algorithm asks the user to input a number
and then searches for it in the array numbers
• What is the value of LENGTH(numbers) – 1 ? 8
• When will "SearchItem found" be output?
Each time it finds a matching item in the array
• Name the type of search algorithm
Linear search
Standard Algorithms
Unit 8 Algorithm design and problem solving

Sorting
• Both people and computers often need data to be
sorted
• Give five types of data that
people need to be sorted
• Give one reason why a
computer would work
more efficiently if a
list was sorted
Standard Algorithms
Unit 8 Algorithm design and problem solving

Sorting
• Types of data that people need sorted
• Index cards/records of customer details
• Directories and dictionaries
• House numbers
• Library books
• Stock in a warehouse
• and many more…
• Reasons why a computer will work more efficiently
with sorted lists
• Computers can use a binary search with sorted lists which is
far more efficient than a linear search – you don’t need to
know the binary search algorithm for IGCSE
Standard Algorithms
Unit 8 Algorithm design and problem solving

Sorting
• Data sets frequently need to be sorted
Rank Name Count Rank Name Count
• Look at the two
data sets of the 1 Oliver 5 390 1 Olivia 4 598
top 10 boys and 2 George 4 960 2 Amelia 3 941
girls names 3 Harry 4 512 3 Ava 3 110

• What other orders 4 Noah 4 107 4 Isla 3 046


could they 5 Jack 3 988 5 Emily 2 676
be sorted in? 6 Leo 3 721 6 Mia 2 490

• Why might you 7 Arthur 3 644 7 Isabella 2 369


need to sort the 8 Muhammad 3 507 8 Sophia 2 344
data sets? 9 Oscar 3 459 9 Ella 2 326

10 Charlie 3 365 10 Grace 2 301


Source: ONS
Standard Algorithms
Unit 8 Algorithm design and problem solving

Sorting
• The data sets could be sorted as follows:
• Count order (which will be the same as the rank order)
• Alphabetical order
• Reverse alphabetical or count order

• The girls and boys data sets could be combined to


show the top 20 names in England and Wales
• Again these could be sorted by count or name
Standard Algorithms
Unit 8 Algorithm design and problem solving

The bubble sort


• Start with the leftmost item
• Compare this item with the one next to it
• If the one next to it is less, swap the items
• Repeat for all the other items
• At the end of one pass through the list, the largest item is at
the end of the list
• Repeat the process until the items are sorted
• Suppose you have a list of numbers to be sorted:

9 5 4 15 3 8 11 2
Standard Algorithms
Unit 8 Algorithm design and problem solving

Bubble sort – First pass


9 5 4 15 3 8 11 2 • Each item is compared
5 9 4 15 3 8 11 2 with the one on its right,
and swapped if it is
5 4 9 15 3 8 11 2
larger
5 4 9 15 3 8 11 2
• At the end of the first
5 4 9 3 15 8 11 2 pass the largest item
5 4 9 3 8 15 11 2 bubbles through to the
end of the list
5 4 9 3 8 11 15 2
• (Orange indicates
5 4 9 3 8 11 2 15
sorted items)
Standard Algorithms
Unit 8 Algorithm design and problem solving

First pass Second pass


9 5 4 15 3 8 11 2 5 4 9 3 8 11 2 15

5 9 4 15 3 8 11 2 4 5 9 3 8 11 2 15

5 4 9 15 3 8 11 2 4 5 9 3 8 11 2 15

5 4 9 15 3 8 11 2 4 5 3 9 8 11 2 15

5 4 9 3 15 8 11 2 4 5 3 8 9 11 2 15

5 4 9 3 8 15 11 2 4 5 3 8 9 11 2 15

5 4 9 3 8 11 15 2 4 5 3 8 9 2 11 15

5 4 9 3 8 11 2 15
Standard Algorithms
Unit 8 Algorithm design and problem solving

Second pass Third pass


5 4 9 3 8 11 2 15 4 5 3 8 9 2 11 15

4 5 9 3 8 11 2 15 4 5 3 8 9 2 11 15

4 5 9 3 8 11 2 15 4 3 5 8 9 2 11 15

4 5 3 9 8 11 2 15 4 3 5 8 9 2 11 15

4 5 3 8 9 11 2 15 4 3 5 8 9 2 11 15

4 5 3 8 9 11 2 15 4 3 5 8 2 9 11 15

4 5 3 8 9 2 11 15
Standard Algorithms
Unit 8 Algorithm design and problem solving

Third pass Fourth pass


4 5 3 8 9 2 11 15 3 4 5 8 2 9 11 15

4 5 3 8 9 2 11 15 3 4 5 8 2 9 11 15

4 3 5 8 9 2 11 15 3 4 5 8 2 9 11 15

4 3 5 8 9 2 11 15 3 4 5 8 2 9 11 15

4 3 5 8 9 2 11 15 3 4 5 2 8 9 11 15

4 3 5 8 2 9 11 15
Standard Algorithms
Unit 8 Algorithm design and problem solving

Fourth pass Fifth pass


3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15

3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15

3 4 5 8 2 9 11 15 3 4 5 2 8 9 11 15

3 4 5 8 2 9 11 15 3 4 2 5 8 9 11 15

3 4 5 2 8 9 11 15
Standard Algorithms
Unit 8 Algorithm design and problem solving

Fifth pass Sixth pass


3 4 5 2 8 9 11 15 3 4 2 5 8 9 11 15

3 4 5 2 8 9 11 15 3 4 2 5 8 9 11 15

3 4 5 2 8 9 11 15 3 2 4 5 8 9 11 15

3 4 2 5 8 9 11 15

Sixth pass Seventh pass


3 4 2 5 8 9 11 15 3 2 4 5 8 9 11 15

3 4 2 5 8 9 11 15 2 3 4 5 8 9 11 15

3 2 4 5 8 9 11 15 Seven passes through the list of eight


numbers ensures that they are sorted
Standard Algorithms
Unit 8 Algorithm design and problem solving

Worksheet 4
• Complete Task 3, Task 4 and Task 5
on Worksheet 4
Standard Algorithms
Unit 8 Algorithm design and problem solving

Plenary
• A user enters the following numbers:
• 9, 17, 3, 5, 29

• Work in pairs to describe an algorithm to find:


• the largest number
• the average of the numbers
Standard Algorithms
Unit 8 Algorithm design and problem solving

Plenary
• A user enters the following numbers:
• 9, 17, 3, 5, 29

Largest number algorithm Average algorithm


Largest  0 NumberOfItems  0
WHILE MoreNumbers DO Total  0
INPUT Num WHILE MoreNumbers DO
IF Num > Largest INPUT Num
THEN Total  Total + Num
Largest  Num NumberOfItems  NumberOfItems +
ENDIF 1
ENDWHILE ENDWHILE
Average  Total / NumberOfItems
Standard Algorithms
Unit 8 Algorithm design and problem solving

Copyright

© 2021 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.

Licence agreement

This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.

The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.

You might also like