1.1 Algorithms
1.1 Algorithms
An algorithm is a precise step-by-step set of instructions for solving a problem or carrying out a task
They are often used to improve efficiency by removing the need for human input
A computer following an algorithm can decide what decision to make far more quickly than a human
Book
Algorithms
Link: https://www.youtube.com/watch?v=jWWOM53Zh20
Algorithm Constructs
The three constructs used in algorithms are: sequence, selection and iteration
Once an algorithm has been written, it can be reused with slight changes for solving similar problems, which is much
quicker than starting over each time
Book
Algorithm Constructs
Sequence Instructions are carried out in order one after the other
Iteration Instructions are repeated until a desired outcome or a condition is met (sometimes called a loop)
Book
Algorithms
1. Slice bread
2. Put bread in toaster 1. Set number to guess (11)
3. Turn on the toaster 2. Other person guesses
4. Wait until toaster pops 3. If too high, say too high and try again
5. Check if toasted enough, if not re-toast 4. If too low, say too low and try again
6. Add butter 5. If correct say correct
7.Add jam
Activity continued
3 Getting from room 4.1 to room 1.14 4 Steps taken when the fire alarm goes off
On the next four slides we will look at the same algorithm written in each method
Types of Algorithms
1 Written Description
Book
Types of Algorithms
2 Flowchart
A graphical method of showing the flow of information using a series of symbols and arrows
Book
Types of Algorithms
3 Pseudocode
Uses structured English and keywords with a heavy focus on the order of the instructions (the logic)
Book
Types of Algorithms
4 Program Code
Book
Algorithms
Link: https://www.youtube.com/watch?v=McM9YCeyt_Q
Flowchart Symbols
Start Terminal
Start Terminal
Print “Hello
Output
world” Print “Mr” Output
End Terminal
Print “Lyon” Output
End Terminal
Book
Flowcharts
Start Terminal
Process
Alarm Rings
Ready NO
Hit snooze
to get Process
button
up? Decision
YES
End Terminal
Book
Flowcharts
Start Terminal
Input mark1,
mark2, mark3
Input
Decision
NO
Is mark1 > Is mark2 > NO
mark2? mark3?
Decision
YES YES Output Output
NO Output Output
Output Is mark1 > mark2 mark3
Output mark3 mark3?
Decision
YES
End End
End
Output
Terminal Terminal
Terminal mark1 Output
On the next slide is a flowchart used by a theme park to calculate how much a group of people should pay for entry
Part 1
Flowcharts
Start
1
2
Example YES
Is age >=
Flowchart Charge = 0 60? Charge = £9
(Difficult 2) Total = 0
Number = 0 NO
Is there YES
Charge = £10 another
Number = Number + 1 customer in
the group?
NO
Input age
YES
Is Number
> 4? Total = Total - £10
YES
Is age < Charge = £5 NO
13? Number = Number - 1
Output
Total
NO
Part 2 1 2
End
Activity
2 Label where each of the three constructs (sequence, selection and iteration) are used
3 Describe how the algorithm calculates the total amount that should be paid
The Smith family are visiting the park. The family consists of two children, one aged 8 and one aged 10, their two
4
parents and their grandfather, who is aged 65. Use the algorithm to calculate how much the family should pay
Part 3
Activity
Answers
The regular charge is £10, but children under 13 pay £5 and people aged 60 or above pay £9. A group consisting of
3
five or more adults get a discount of £10.
(2 × £5) + (2 × £10) + £9 = £39 (They are not eligible for a group discount: there are five people, but the
4
variable 'number' comes to 3, not 5. Why is that?)
Book
Activity
Input
Input
width
Input Input
height
Area = width
Process
* height
Output
Output
area
End Terminal
Sheet
Activity
Start Terminal
Making a Cup of Tea
Fill up and turn
kettle on Process Guessing a Number Between 1 and 20 Written Description
Process
Kettle
NO Set number to 11
boiled? Wait longer User inputs a guess
Decision
If guess is the same as the number, display ‘well done’
YES Else output ‘unlucky, try again’ and allow another guess
Add tea bag and pour
Process
water into cup
End Terminal
Sheet
Activity
Set number to 11
Input guess Input User inputs a guess
If guess is the same as the number, display ‘well done’
Decision Else output ‘unlucky, try again’ and allow another guess
Does Output
guess =
number?
“unlucky, try
NO Output
again”
YES
Output
“well done” Output
End Terminal
Sheet
Activity
Sheet
Pseudocode
Does not use strict syntax, it just needs to be clear and consistent
Book
Pseudocode
Use of Pseudocode
Uses commands, keywords and structures similar to those found in computer languages
Cannot be understood by computers, but is used to develop the logic of a program without needing to worry about the
syntax
A human can follow the logic of an algorithm even if there are spelling mistakes, missing brackets or quotation marks
etc.
A solution in pseudocode can then be converted into a high-level language such as Python
Sheet
Pseudocode
Start
Output END
total
End
Pseudocode
Start
SEND ‘Enter age’ TO DISPLAY
Enter age
RECEIVE age FROM (INTEGER) KEYBOARD
Enter your age
NO
If the age is larger than 17 set If age >
type = child
IF age > 17 THEN
17
the type to adult SET type TO adult
Output
Else if the age is less than or YES ‘child’
ELSE
equal to 17 set the type to type = adult SET type TO child
End
child END IF
Output
Display the type ‘adult’
SEND type TO DISPLAY
End
END
Algorithms
Purpose of an Algorithm
Purpose of an Algorithm
ACTIVITY
Start Terminal
- processes
Iteration - decisions
weight = weight
of parcel Process - terminals
- iteration
Decision
is NO - input/outputs
weight Output
cost = 2
>2 cost
YES Process
Output
excess
Process
= weight - 2
cost
= 2 + (excess * 3) Process
Algorithms
Purpose of an Algorithm
Flowchart Pseudocode
Purpose of an Algorithm
Pseudocode Explanations
SET parcel to ‘y’ While loop ensures the program keeps running
WHILE parcel = ‘y’ DO
RECEIVE weight FROM (FLOAT) KEYBOARD User enters the weight of the parcel
IF weight <= 2 THEN
SET cost TO 2 If/else statement to work out the cost of the
ELSE parcel depending on if the weight is above or
SET excess TO weight – 2 below 2
SET cost TO 2 + (excess * 3)
END IF
SEND cost TO DISPLAY Outputs the final cost depending on the weight
SEND ‘press y for another parcel’ TO DISPLAY User is asked if they wish to run the program again
RECEIVE parcel FROM (STRING) KEYBOARD
END WHILE Programs ends if ‘y’ is not entered
Algorithms
Purpose of an Algorithm
A trace table is used to identify the values of variables, inputs and outputs of a program at each stage
Book
Output of an Algorithm
Book
Activity
Book
Activity
Book
Errors
Syntax errors occur when algorithms are converted into program code
Pseudocode does not have syntax or runtime errors, so the user can focus on the logic of the program
Sheet
Errors
SET index TO 1
Question
WHILE index < 10 DO
SEND index TO DISPLAY
END WHILE
SET index TO 1
WHILE index < 10 DO
Answer
SEND index TO DISPLAY
SET index TO index + 1
END WHILE
Sheet
Errors
SET index TO 1
Question
WHILE index < 10 DO
SEND index TO DISPLAY
SET index TO index - 1
END WHILE
SET index TO 1
WHILE index < 10 DO
Answer
SEND index TO DISPLAY
SET index TO index + 1
END WHILE
Sheet
Errors
SET index TO 1
Question
WHILE index < 1 DO
SEND index TO DISPLAY
SET index TO index + 1
END WHILE
Answer
SEND index TO DISPLAY
SET index TO index + 1
END WHILE
Sheet
Standard Algorithms
You know an algorithm is a precise step-by-step set of instructions for solving a problem or carrying out a task
Two of the most common tasks in computer programs are sorting data into a particular order and searching for
particular items of information
Standard Algorithms
7,9,12,4,6,3,7,98,9,2,8,4,5,6,3,2,1,4,7,8,1,1,2,3,6,5,9,8,7,4,1,5,8,56,11,45,101
There might be thousands of items of stored data and searching for information would be very inefficient if the data
was not sorted
All sorting and searching algorithms work on lists of data. But variables only store a single value, so we need to use an
array, sometimes called a data container
Book
Standard Algorithms
Sorting Algorithms
Searching Algorithms
Sorting Algorithms
As sorting is such a widely used procedure, many algorithms have been created to carry it out
Book
Activity
Unsorted
2 1 3 6 5 4 1 2 3 5 4 6
List
1 2 3 6 5 4 1 2 3 5 4 6
1 2 3 6 5 4 1 2 3 5 4 6
First pass Second pass
1 2 3 6 5 4 1 2 3 5 4 6
1 2 3 5 6 4 1 2 3 4 5 6
Sorted
1 2 3 5 4 6 List 1 2 3 4 5 6
Activity
92 30 56 42
30 56 42 92 Pass 1
30 42 56 92 Pass 2
30 42 56 92 Pass 3
Activity
76 85 98 27
76 85 27 98 Pass 1
76 27 85 98 Pass 2
27 76 85 98 Pass 3
27 76 85 98 Pass 4
Activity
99 35 59 26
35 59 26 99 Pass 1
35 26 59 99 Pass 2
26 35 59 99 Pass 3
26 35 59 99 Pass 4
Standard Algorithms
The bubble sort algorithm works through a list, comparing pairs of values and swapping them if necessary
The process is repeated until there are no swaps during a pass, indicating that all items must be in the correct order
Book
Standard Algorithms
https://goo.gl/FrEQSK
If you pass 50 points you have mastered bubble sort
6 3 5 1 8 2 4 7 Unsorted List
6 3 5 1 8 2 4 7
6 3 5 1 8 2 4 7
6 3 5 1 8 2 4 7
3 6 1 5 2 8 4 7
1 3 5 6 2 4 7 8
1 2 3 4 5 6 7 8 Sorted List
Activity
12 9 13 4 1 22 7 11 Unsorted List
12 9 13 4 1 22 7 11
12 9 13 4 1 22 7 11
12 9 13 4 1 22 7 11
9 12 4 13 1 22 7 11
4 9 12 13 1 7 11 22
1 4 7 9 11 12 13 22 Sorted List
Standard Algorithms
A merge sort divides a list into two smaller lists and then divides these until the size of each list is one
The left and right lists are recursively (repeatedly) merged together with the items in the correct order
Book
Standard Algorithms
Searching Algorithms
Like sorting, some methods of searching are more efficient than others
Book
Activity
3 8 1 4 7 6 9 2 12 11 10 5
3 7 1 4 11 6 28 2 12 85 17 5
How does it find the number 11? Searching for the number 11
Standard Algorithms
Linear search starts at the first element in the list and checks every element until it finds the one it is looking for
If the value it was searching for was at the end of the list it would take a long time
Book
Activity
0 1 2 4 5 6 8 10 12 13 14 15
What do you notice about how the numbers are arranged? Searching for the number 14
Activity
18 19 20 21 22 23 24 25 26 27 28 29
Binary search is a good way of searching lists that have been sorted (also called pre-processed)
At each step it halves the number of items it needs to look at until it finds the correct item
Book