AQA 7517, section 4.4.1.2
What is an algorithm?
An algorithm is a sequence of steps that can be followed to complete a task, and that always terminates (comes to an end).
Two points are important:
- The steps must be precise enough that anyone (or any computer) can follow them without guessing.
- The algorithm must stop. A set of steps that could go on forever isn't an algorithm.
A recipe, directions to a friend's house and a long division method are all algorithms.
Pseudo-code
Pseudo-code is a way of writing an algorithm that looks like program code, but isn't tied to any particular language. It lets you plan the logic without worrying about syntax. You should be able to use the standard constructs:
| Construct | Meaning | Pseudo-code example |
|---|---|---|
| Sequence | Steps in order | a ← 5 then b ← 7 |
| Assignment | Store a value | total ← total + x |
| Selection | Choose a path | IF x > 10 THEN ... ELSE ... ENDIF |
| Iteration | Repeat | WHILE ... ENDWHILE, FOR ... NEXT |
Algorithm: find the largest number in a list.
largest ← list[0]
FOR i ← 1 TO length - 1
IF list[i] > largest THEN
largest ← list[i]
ENDIF
NEXT i
OUTPUT largest
The arrow ← means "is assigned the value of". The exact layout can vary, as long as the steps are clear and unambiguous.
Hand-tracing with a trace table
To hand-trace an algorithm, you act as the computer: follow each step with sample data and write down how the variables change. A trace table has a column for each variable, and a row for each change.
Trace the algorithm above with list = [4, 9, 2, 7]:
| i | list[i] | list[i] > largest? | largest |
|---|---|---|---|
| 4 | |||
| 1 | 9 | Yes | 9 |
| 2 | 2 | No | 9 |
| 3 | 7 | No | 9 |
The output is 9.
Converting pseudo-code to a program
Once the algorithm is clear, it can be written in a programming language. Each construct has a direct equivalent.
def find_largest(items):
largest = items[0]
for i in range(1, len(items)):
if items[i] > largest:
largest = items[i]
return largest
print(find_largest([4, 9, 2, 7])) # 9
Arguing for correctness and efficiency
You should be able to explain how and why a program works, and argue that it is correct and efficient, using three kinds of evidence:
- Logical reasoning: explain why each step is right. For
find_largest,largestalways holds the biggest value seen so far, so after the last item it holds the biggest of all. - Test data: run it with normal values, boundary values (a list of one item, equal items, negative numbers) and check the results match what you expect.
- User feedback: ask real users whether the program does what they need, and whether it is easy to use.
Efficiency means how much time and memory the algorithm uses. find_largest looks at each item once, so the time grows in proportion to the list length (see 4.4.4.3).
Worked example
Task: write an algorithm to calculate the average of numbers entered, stopping when the user enters 0.
total ← 0
count ← 0
INPUT n
WHILE n ≠ 0
total ← total + n
count ← count + 1
INPUT n
ENDWHILE
IF count > 0 THEN
OUTPUT total / count
ELSE
OUTPUT "No numbers entered"
ENDIF
Trace with the inputs 4, 6, 0:
| n | total | count | Output |
|---|---|---|---|
| 4 | 4 | 1 | |
| 6 | 10 | 2 | |
| 0 | 10 | 2 | 5 |
The algorithm terminates, because the user eventually enters 0. The IF count > 0 selection stops it dividing by zero if the first number entered is 0.
Exam tip: in a trace table, add a new row each time a variable changes, and keep columns for every variable and for the output. Don't skip steps, even obvious ones.
Check yourself
What is an algorithm?
A sequence of steps that can be followed to complete a task and that always terminates.
Name the four standard constructs used in pseudo-code
Sequence, assignment, selection and iteration.
What is the purpose of a trace table?
To follow an algorithm by hand and record how the values of variables (and outputs) change at each step, so you can check what it does or find errors.
Give three ways of arguing that a program is correct
Logical reasoning about why the steps work, testing with suitable data (normal, boundary and invalid), and getting feedback from users.