Edexcel International Advanced Level Computer Science, point 5.1.1
Definition and function
An algorithm is a finite, ordered set of clear steps that solves a problem or carries out a task. It describes how to get from the inputs to the outputs. Algorithms are the core of computer science: a program is an algorithm written in a programming language.
Why needed: you must plan the solution before coding, so that it can be checked, shared and turned into any language.
Characteristics of a good algorithm
- Unambiguous: each step is precise, with only one meaning.
- Finite: it ends after a limited number of steps.
- Has inputs and outputs.
- Correct: it gives the right result for every valid input.
- Efficient: it uses a reasonable amount of time and memory.
- General: it works for any suitable input, not just one case.
The three building blocks
Every algorithm is built from just three constructs:
| Construct | Meaning | In a flowchart |
|---|---|---|
| Sequence | Steps carried out one after another, in order | Boxes joined by arrows in a line |
| Selection | A choice between paths, depending on a condition (if) |
A decision diamond with two labelled exits |
| Iteration | Steps repeated while (or until) a condition holds (for, while) |
A flow line that loops back to an earlier step |
Expressing algorithms
The same algorithm can be shown in several ways. You must be able to interpret, complete and develop each.
1. In words (structured English)
- Set total to 0 and count to 0.
- Ask for a number.
- While the number is not −1:
- add it to total
- add 1 to count
- ask for the next number.
- If count is greater than 0, output total divided by count. Otherwise, output "No numbers entered".
2. In a diagram or flowchart
A flowchart uses standard symbols (given in Appendix 2 of the specification):
| Symbol | Meaning |
|---|---|
| Terminator (rounded box) | Start or end |
| Input/output (parallelogram) | Reading or showing data |
| Process (rectangle) | A calculation or assignment |
| Decision (diamond) | A yes/no question, with labelled exits |
| Subprocess (rectangle with double sides) | A call to another procedure |
| Flow lines (arrows) | The order of steps |
Guidelines for drawing a flowchart:
- Have one Start and one End.
- Use arrows to show the direction of flow.
- Give every decision two labelled exits (yes and no).
- Keep the flow running from top to bottom, with as few crossing lines as possible.
3. In code (Python)
total = 0
count = 0
n = int(input("Number (-1 to stop): "))
while n != -1:
total = total + n
count = count + 1
n = int(input("Number (-1 to stop): "))
if count > 0:
print(total / count)
else:
print("No numbers entered")
Which to use? Words and flowcharts show the logic clearly to anyone. Code is exact and can be run. In Unit 1 you interpret and develop algorithms in these forms, and in Unit 2 you convert them into code.
Analysing efficiency
To compare algorithms:
- Count the steps (comparisons, assignments) as the input size grows.
- Time: one loop over the data takes about steps. A loop inside a loop takes about .
- Space: how much extra memory is needed (a copy of the data? extra variables?).
- The best, average and worst cases can differ. A linear search finds the item at once in the best case, but checks every item in the worst case.
(Big O notation, 15.3.1, gives a standard way to describe this.)
Example: finding the largest of numbers needs comparisons (one pass), so it grows in proportion to . Sorting the list first and taking the last would be much more work, so the one-pass method is more efficient.
Finding and correcting logic errors
A logic error is a mistake in the algorithm's reasoning: it runs, but gives the wrong result. To find one, trace it with test data (5.1.2), especially boundary and unusual values.
Example of a faulty algorithm (find the largest number):
largest = 0
for n in numbers:
if n > largest:
largest = n
print(largest)
- Test with
[-5, -2, -9]: the output is 0, which isn't in the list. Error:largeststarts at 0, which is larger than every number in this list. - Fix: start with the first item.
largest = numbers[0]
for n in numbers[1:]:
if n > largest:
largest = n
Other common logic errors: off-by-one (< instead of <=), the wrong operator, not resetting a counter, a loop that never ends, and conditions tested in the wrong order.
Worked example
Task: this algorithm should output the mean of the three numbers a, b and c, but is wrong. Find and correct the error, and say how to test it.
mean = a + b + c / 3
- Error: operator precedence: division happens first, so it calculates .
- Fix:
mean = (a + b + c) / 3. - Test: with , , , the expected mean is 6. The faulty line gives , which shows the error, and the fixed line gives 6. ✓
Exam tip: when given an algorithm in any form, trace it with real values rather than guessing. When finding errors, say what is wrong, why it gives the wrong result, and how to fix it. For "efficiency", mention how the number of steps grows with the size of the input.
Check yourself
What is an algorithm?
A finite, ordered set of clear steps that solves a problem or carries out a task.
Give three ways of expressing an algorithm
Any three of: words (structured English), a diagram, a flowchart, pseudocode, code.
What shape is a decision in a flowchart?
A diamond, with labelled exits such as yes and no.
What is a logic error, and how do you find it?
A mistake in the algorithm's reasoning that makes it give wrong results without an error message. You find it by tracing the algorithm with test data, especially boundary values.
Give two qualities of a good algorithm
Any two of: unambiguous; finite; correct; efficient; general.