AQA 7517, section 4.4.4.1
There are usually several different algorithms for solving the same problem. To choose between them we need a fair way to compare them. Timing a program on one computer isn't fair, because the result depends on the machine, the language and what else is running.
Complexity as a function of problem size
Instead, we express the complexity of an algorithm as a function of the size of the problem. The size of the problem (usually written ) is the key issue: for example, the number of items in a list to be searched or sorted.
We then ask: as gets larger, how fast does the work grow?
| Algorithm | Work for items |
|---|---|
| Linear search | up to comparisons |
| Binary search | about comparisons |
| Bubble sort | about comparisons |
| Merge sort | about steps |
This compares the shape of the growth, not exact times, so it holds on any computer.
Time and space
Algorithms can be more efficient in two ways.
- Time-wise: they take fewer steps, and so run faster. Binary search is more efficient than linear search in time.
- Space-wise: they use less memory. Bubble sort sorts in place and needs almost no extra memory, whereas merge sort needs extra space for the merged lists.
Efficiently implementing an automated abstraction means designing data models and algorithms that run quickly while taking up the minimum amount of resources, such as memory.
Often there's a trade-off: a faster algorithm may need more memory, and a memory-saving one may be slower.
Why the size of the problem matters
For small , almost any algorithm is fast enough. The differences only show as grows.
Suppose each step takes one microsecond (a millionth of a second):
| Linear search () | Binary search () | Bubble sort () | Merge sort () | |
|---|---|---|---|---|
| 10 | 10 µs | about 3 µs | 100 µs | about 33 µs |
| 1 000 | 1 ms | about 10 µs | 1 s | about 10 ms |
| 1 000 000 | 1 s | about 20 µs | about 11.6 days | about 20 s |
For a million items, bubble sort takes about 11.6 days, and merge sort takes about 20 seconds. The same task, with a different algorithm, takes seconds or days. That's why complexity matters, and why the best algorithm for small data may not be the best for large data.
Counting steps
To compare algorithms, count how many basic steps they perform for an input of size .
def count_comparisons(items, target):
steps = 0
for item in items:
steps = steps + 1
if item == target:
break
return steps
data = list(range(1000))
print(count_comparisons(data, 999)) # 1000: the worst case
print(count_comparisons(data, 0)) # 1: the best case
The count depends on the case: best, average or worst. Usually the worst case is quoted, because it guarantees that the algorithm will never take longer than that.
Worked example
Task: a program must find a name in a list of 1 000 000 sorted names. Compare linear and binary search.
- Linear search: in the worst case it checks all 1 000 000 names.
- Binary search: each check halves the list, so it needs at most about 20 comparisons ().
Binary search is far more efficient time-wise, and neither uses much extra memory. The cost is that the list must be sorted first. If the list is searched many times, that cost is worth paying.
Exam tip: compare algorithms in terms of the size of the problem (). Say whether you're talking about time or space, and say which case you mean (usually the worst). Avoid saying "it is faster" without saying what it is faster for, such as large lists.
Check yourself
Why is complexity expressed as a function of problem size rather than as a measured time?
A measured time depends on the computer, the language and other factors, whereas how the work grows as the problem size grows is the same on any machine and allows a fair comparison.
What are the two ways in which one algorithm can be more efficient than another?
Time-wise (it takes fewer steps) and space-wise (it uses less memory).
Why does the size of the problem matter when choosing an algorithm?
For small inputs most algorithms are fast enough, but as the size grows the differences between them become huge.