T4 Standard Algorithms
T4 Standard Algorithms
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
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
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
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
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
9 5 4 15 3 8 11 2
Standard Algorithms
Unit 8 Algorithm design and problem solving
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
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
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
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
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
3 4 2 5 8 9 11 15 2 3 4 5 8 9 11 15
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
Plenary
• A user enters the following numbers:
• 9, 17, 3, 5, 29
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.