OCR H446, section 2.1.3 (c)
An algorithm is a sequence of steps, and the order matters. The same steps in a different order can give a wrong result or fail to work at all.
Why order matters
Making a cup of tea: boil the water before pouring it. Pouring first, then boiling, doesn't work.
In a program:
# Wrong order: total is used before it has a value
print(total)
total = 10
# Right order
total = 10
print(total)
Dependencies
Step B depends on step A if B needs something that A produces. Dependencies decide the order.
| Step | Needs |
|---|---|
| Read the numbers | Nothing |
| Add them up | The numbers have been read |
| Divide by how many there are | The total |
| Display the mean | The mean has been calculated |
So the order must be: read → add up → divide → display. Steps that don't depend on each other can be done in either order, or at the same time (2.1.5).
Common ordering rules
- A variable must be declared or given a value before it is used.
- Data must be input before it is processed.
- Data must be validated before it is used.
- Data must be sorted before a binary search.
- A file must be opened before it's read and closed after.
- Initialise a counter or total before a loop that updates it.
- Calculate before you output.
Showing the order
Flowchart
Flowchart symbols: rounded for start/stop, parallelogram for input/output, rectangle for a process, diamond for a decision, and arrows show the order.
Pseudocode
total = 0
for i = 1 to 5
n = int(input("Number: "))
total = total + n
next i
print(total / 5)
Trace table
A trace table follows the values of variables step by step, and checks that the order produces the correct results:
| Step | i |
n |
total |
|---|---|---|---|
| Start | 0 | ||
| 1 | 1 | 4 | 4 |
| 2 | 2 | 6 | 10 |
| 3 | 3 | 5 | 15 |
| 4 | 4 | 3 | 18 |
| 5 | 5 | 7 | 25 |
| Output | 5 (25 / 5) |
Order inside loops and decisions
- Inside a loop, the order of the statements is repeated each time. A counter updated before or after use gives different results.
- Conditions should be checked in the right order: test the most specific case first.
# Wrong: a mark of 90 gives "pass" because the first test is true
if mark >= 50:
grade = "pass"
elif mark >= 80:
grade = "distinction"
# Right: the highest threshold is tested first
if mark >= 80:
grade = "distinction"
elif mark >= 50:
grade = "pass"
Worked example
Task: put the steps to find the largest of a list of numbers into the correct order.
Steps jumbled:
- A. Output
largest - B. For each number in the list
- C. Set
largestto the first number - D. If the number is greater than
largest, setlargestto the number
Order and reasons:
- C:
largestmust have a value before anything is compared with it. - B: go through the list (this repeats step D).
- D: the comparison happens inside the loop.
- A: output only after the loop is finished, otherwise the answer might not be the largest yet.
largest = numbers[0] # C
for n in numbers: # B
if n > largest: # D
largest = n
print(largest) # A
Exam tip: explain why a step must come before another, using the idea of dependency ("the total must be initialised before it's added to"). When you trace an algorithm, show each step in order, and update only one variable at a time.
Check yourself
Why does the order of steps in an algorithm matter?
A step may need the result of an earlier step, so changing the order can give wrong results or make the algorithm fail.
What is a dependency between two steps?
One step needs something that the other produces, so the second can't happen until the first has been done.
Why must a total be set to 0 before a loop that adds to it?
Otherwise it has no starting value (or an unknown one), so the additions give a wrong result or an error.
Name two ways of showing the order of steps in an algorithm
Any two of: a flowchart, pseudocode, a trace table, a structured list of steps.