AQA 7517, section 4.4.4.3
Big-O notation describes how the time (or memory) an algorithm needs grows as the size of the input, , grows. It ignores the speed of your computer and focuses on the shape of the growth.
We write with the fastest-growing part of the function inside the brackets, such as or . It describes an upper bound on growth, normally for the worst case.
Common orders of complexity
| Notation | Name | Typical example |
|---|---|---|
| Constant | Reading an array element by its index | |
| Logarithmic | Binary search | |
| Linear | Linear search | |
| Linearithmic | Merge sort | |
| Polynomial (quadratic) | Bubble sort, with nested loops | |
| Exponential | Checking every subset of a set |
Other polynomials, such as , are also polynomial time. sits between linear and quadratic. From best to worst:
The graph shows how quickly the number of steps grows as gets bigger:
What each means when the input doubles:
| Complexity | If doubles, the time... |
|---|---|
| stays the same | |
| increases by a small fixed amount | |
| doubles | |
| becomes 4 times bigger | |
| gets squared (a huge increase) |
Rules for working it out
- Drop constants: becomes .
- Keep only the fastest-growing term: becomes , because for large the term dominates.
- Usually quote the worst case, unless asked otherwise.
Deriving the time complexity of an algorithm
Look at the structure of the code and count how many times the steps run in terms of .
Constant:
The work doesn't depend on .
def first(items):
return items[0] # one step, however long the list
Linear:
A single loop that goes through the data once.
def total(items):
t = 0
for x in items: # runs n times
t = t + x
return t
Polynomial (quadratic):
A loop inside another loop, both running about times.
def count_pairs(items):
count = 0
for a in items: # n times
for b in items: # n times for each a
count = count + 1
return count # n x n steps
Logarithmic:
The problem is halved each time round.
def halvings(n):
steps = 0
while n > 1:
n = n // 2 # halves each time
steps = steps + 1
return steps # about log2(n)
Exponential:
The work doubles each time increases by 1. A classic example is a recursive function that calls itself twice.
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2) # two calls each time
fib(30) takes over a million calls, and fib(50) is already impractical.
Using the rules: a combined example
def example(items):
n = len(items)
print(items[0]) # 1 step: O(1)
for x in items: # n steps: O(n)
print(x)
for a in items: # n x n steps: O(n^2)
for b in items:
print(a, b)
The total is steps. Keeping only the fastest-growing term, the time complexity is .
Worked example
Task: give the Big-O complexity of each, and justify it.
- Looking up a value in a dictionary by key (via a hash table): about one step whatever the size, so .
- Finding the largest value in an unsorted list: every item must be checked once, so .
- Checking a list for duplicates by comparing every pair: a loop inside a loop, so .
- Binary search of a sorted list: the list halves each step, so .
Exam tip: when you derive a complexity, point to the code feature that causes it: "one loop through the data, so ", "nested loops, so ", "halves each time, so ". Always simplify: drop constants and keep only the largest term.
Check yourself
What is the time complexity of a loop nested inside another loop, both running n times?
The inner loop runs times for each of the outer iterations, giving steps, so it is .
Simplify
: drop the constants and keep the fastest-growing term.
Give an algorithm with logarithmic time complexity
Binary search. It halves the remaining items each step.
If an algorithm is and the input doubles, what happens to the running time?
It becomes roughly four times longer.