A levels Computer Science · Algorithms
Big-O notation and complexity
1Why measure complexity
Two algorithms can solve the same problem with very different efficiency. Complexity describes how the resources needed grow as the input size n grows:
- time complexity: how the number of steps grows;
- space complexity: how the memory needed grows.
We measure growth, not seconds, because seconds depend on the computer, the language and other programs running.
Check yourself
Why is complexity measured in steps rather than seconds?
Show the answer
Seconds depend on the hardware and language, but growth in steps does not
Fair comparison.
2Big-O notation
Big-O notation gives an upper bound on how the work grows, keeping only the dominant term and dropping constants.
Simplifying
- An algorithm takes 3n² + 5n + 20 steps.
- As n grows large, 3n² is much bigger than the other terms.
- Drop the constant 3: the complexity is O(n²).
Big-O usually describes the worst case, because that is a guarantee: the algorithm will never be slower than this.
Check yourself
What is the Big-O of 4n + 2n log n + 7?
Show the answer
O(n log n)
n log n grows fastest.
3Common complexities
How the number of steps grows with n (O(1) would be a flat line along the bottom). Exponential and quadratic algorithms quickly become far slower.
| Big-O | Name | Example | Steps when n = 1000 |
|---|---|---|---|
| O(1) | constant | accessing an array element | 1 |
| O(log n) | logarithmic | binary search | about 10 |
| O(n) | linear | linear search | 1000 |
| O(n log n) | linearithmic | merge sort | about 10 000 |
| O(n²) | polynomial (quadratic) | bubble sort | 1 000 000 |
| O(2ⁿ) | exponential | trying every subset | about 10³⁰¹ |
| O(n!) | factorial | brute-force travelling salesman | astronomically large |
Check yourself
If an O(n²) algorithm takes 2 s for 1000 items, about how long for 3000 items?
Show the answer
18 s
3² = 9 times longer.
4Working out the complexity of code
- A single loop over n items: O(n).
- A loop inside a loop, both over n: O(n²).
- A loop that halves the problem each time: O(log n).
- Statements one after another: add, then keep the biggest.
for i in range(n): # n times
for j in range(n): # n times for each i
print(i, j) # O(n²) in totalwhile n > 1: # halves each time
n = n // 2 # O(log n)
Mixed code
- A loop of n steps, followed by a nested loop of n × n steps.
- Total = n + n², and n² dominates.
- Complexity = O(n²).
Check yourself
What is the complexity of a loop of n steps containing a binary search of n items?
Show the answer
O(n log n)
n × log n.
5Tractable and intractable problems
- A tractable problem has an algorithm that runs in polynomial time (O(nᵏ) or better), so it can be solved in a reasonable time even for large n.
- An intractable problem has no known polynomial-time algorithm, only exponential or worse. It can be solved in theory, but takes far too long for large inputs.
the travelling salesman problem (shortest round trip through every city). Brute force checks (n − 1)!/2 routes: for 20 cities, about 6 × 10¹⁶.
For intractable problems we often use a heuristic: a rule of thumb that gives a good enough answer quickly, but not always the best one. For example, "always go to the nearest unvisited city next".
Check yourself
What does a heuristic give up to run quickly?
Show the answer
The guarantee of finding the best answer
Good enough.
6Computable and non-computable problems
Some problems cannot be solved by any algorithm, however much time is allowed. These are non-computable.
The best-known is the halting problem: is there a program that can decide, for any program and input, whether that program will eventually stop? Alan Turing proved in 1936 that no such program can exist.
| Class | Meaning | Example |
|---|---|---|
| tractable | solvable in polynomial time | sorting |
| intractable | solvable, but no known polynomial algorithm | travelling salesman |
| non-computable | no algorithm can solve it at all | halting problem |
Check yourself
What did Turing prove about the halting problem?
Show the answer
No algorithm can decide for every program and input whether it halts
Undecidable.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera