impera Open Impera

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?

  • Steps are always smaller numbers, so they are easier to compare

  • Seconds cannot be measured accurately enough for small programs

  • Computers do not have clocks that can time individual algorithms

  • Seconds depend on the hardware and language, but growth in steps does not

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.

Worked example

Simplifying

  1. An algorithm takes 3n² + 5n + 20 steps.
  2. As n grows large, 3n² is much bigger than the other terms.
  3. 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?

  • O(4n)

  • O(n log n)

  • O(n)

  • O(1)

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-ONameExampleSteps when n = 1000
O(1)constantaccessing an array element1
O(log n)logarithmicbinary searchabout 10
O(n)linearlinear search1000
O(n log n)linearithmicmerge sortabout 10 000
O(n²)polynomial (quadratic)bubble sort1 000 000
O(2ⁿ)exponentialtrying every subsetabout 10³⁰¹
O(n!)factorialbrute-force travelling salesmanastronomically large
Check yourself

If an O(n²) algorithm takes 2 s for 1000 items, about how long for 3000 items?

  • 6 s

  • 18 s

  • 4 s

  • 9 s

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 total

while n > 1: # halves each time
n = n // 2 # O(log n)

Worked example

Mixed code

  1. A loop of n steps, followed by a nested loop of n × n steps.
  2. Total = n + n², and n² dominates.
  3. Complexity = O(n²).
Check yourself

What is the complexity of a loop of n steps containing a binary search of n items?

  • O(n²)

  • O(n)

  • O(log n)

  • O(n log n)

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.
Example

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?

  • The use of loops and recursion

  • Any answer at all, good or bad

  • Correct syntax in the program

  • The guarantee of finding the best answer

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.

ClassMeaningExample
tractablesolvable in polynomial timesorting
intractablesolvable, but no known polynomial algorithmtravelling salesman
non-computableno algorithm can solve it at allhalting problem
Check yourself

What did Turing prove about the halting problem?

  • It only applies to programs that contain infinite loops

  • It is tractable, but only with a very fast computer

  • No algorithm can decide for every program and input whether it halts

  • It can be solved in polynomial time for most programs and inputs

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