AQA GCSE Computer Science (8525), section 3.1.2 (Efficiency of algorithms)
More than one algorithm
There is often more than one algorithm that solves the same problem. For example, you can find a name in a list by checking every name, or, if the list is in alphabetical order, by repeatedly looking at the middle. Both give the right answer, but one is much quicker.
Efficiency
An algorithm is more efficient if it solves the problem using less time (fewer steps). In the exam, you only need to think about time efficiency. You do not need to use formal methods such as Big O notation.
You can compare two algorithms by counting how many steps they need for the same problem.
Example: adding up the numbers from 1 to n
Algorithm A adds the numbers one at a time:
total ← 0
FOR i ← 1 TO n
total ← total + i
ENDFOR
For this loop runs 1000 times.
Algorithm B uses a formula:
total ← n * (n + 1) DIV 2
This takes one step, whatever the value of .
Both give the same answer, but B is far more efficient, especially for large .
n = 1000
steps_a = 0
total_a = 0
for i in range(1, n + 1):
total_a += i
steps_a += 1
total_b = n * (n + 1) // 2
print(total_a, total_b, steps_a) # 500500 500500 1000
Example: searching
To find a value in a sorted list of 1000 items:
| Algorithm | Most checks needed |
|---|---|
| Linear search | 1000 |
| Binary search | 10 |
So binary search is more efficient for large sorted lists.
Other things that affect speed
The number of steps often grows with the amount of data. An algorithm that is fine for 10 items may be too slow for a million, which is why the efficiency of an algorithm matters.
Summary
- More than one algorithm can solve a problem.
- Some algorithms are more efficient: they need fewer steps, so take less time.
- Compare them by describing or counting the number of steps for the same data.
Common mistake: saying one algorithm is more efficient just because it has fewer lines of code. Efficiency is about how many steps are carried out when it runs, which may be very different from the number of lines.
Exam tip: when asked to compare two algorithms, name the one that is more efficient and say why in terms of fewer steps, comparisons or repetitions for the same problem. Only talk about time.
Check yourself
What does it mean for one algorithm to be more efficient than another?
It solves the same problem using fewer steps, so it takes less time.
Why might a binary search be more efficient than a linear search?
It needs far fewer comparisons to find an item in a large sorted list.
Do you need to use Big O notation for the GCSE?
No, formal comparisons of efficiency are not needed.