Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Complexity and Big-O Notation

4.4.4.1 Comparing algorithms·4.4.4.2 A model for computational complexity·4.4.4.3 Order of complexity

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
6 min
Published
13 June 2026
Updated
1 July 2026
On this page
  1. 1.Why We Need Big-O Notation
  2. 2.Growth Function Classes
  3. 3.Big-O Classes
  4. 4.Deriving the Complexity of an Algorithm
  5. 5.Comparing Algorithms Using Big-O
  6. 6.Space Complexity
  7. 7.Common Exam Mistakes

Key takeaways

  • Big-O notation describes the asymptotic upper bound on a function's growth rate as n approaches infinity, ignoring constant factors and lower-order terms.
  • The growth classes rank O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(n³) < O(2ⁿ) < O(n!) for large n.
  • To derive Big-O: a single loop is O(n), two nested loops O(n²), halving the input O(log n); sequential blocks take the highest order and constant multipliers are dropped.
  • For large n an O(n log n) algorithm is always faster than an O(n²) algorithm regardless of constant factors, so merge sort beats bubble sort at scale.
  • Big-O applies to space as well as time: bubble sort uses O(1) space while merge sort needs O(n), illustrating a time–space trade-off.

Why We Need Big-O Notation

Two algorithms that both solve the same problem may differ drastically in how their running time grows as the input size increases. Big-O notation gives us a way to compare algorithms in terms of how their time (or space) requirements scale, independent of hardware speed or specific constant factors.

Motivating example — sorting 1 000 000 numbers:

  • Bubble sort : roughly operations — would take hours on a modern CPU
  • Merge sort : roughly operations — completes in under a second

The difference is not in the hardware — it is in the algorithmic complexity.

Big-O notation describes the asymptotic upper bound on the growth rate of a function as . It ignores:

  • Constant factors (multiplying by 2 or 100 does not change the class)
  • Lower-order terms ( is , not )

Growth Function Classes

AQA requires knowledge of these growth functions:

NameFormulaExample
ConstantAccessing an array element by index
LogarithmicBinary search; each step halves the problem
LinearLinear search; reading every element once
PolynomialBubble sort ; matrix multiply
ExponentialBrute-force password cracking; subset enumeration
FactorialBrute-force travelling salesman (all permutations)

Growth comparison — relative speed for large :

Plotting the lower classes against input size shows how steeply each one pulls away from the others as grows:

103101001 024
100710010 000
1 000101 0001 000 000too large

Big-O Classes

The five Big-O classes AQA requires:

— Constant: running time does not depend on input size.

// Access the first element of an array — always one step
OUTPUT arr[1]

— Logarithmic: input size is halved (or reduced by a constant factor) each step.

// Binary search — halves the search space each iteration

— Linear: one operation per input element.

// Sum all elements of an array
total ← 0
FOR i ← 1 TO n
    total ← total + arr[i]
NEXT i

— Polynomial: nested loops. Two nested loops → . Three → .

// Compare every pair of elements — O(n²)
FOR i ← 1 TO n
    FOR j ← 1 TO n
        IF arr[i] = arr[j] THEN ...
    NEXT j
NEXT i

— Exponential: the work doubles (or multiplies) with each unit increase in . Grows too fast to be practical beyond small inputs.

Deriving the Complexity of an Algorithm

Rules for deriving Big-O:

  1. Single loop over elements
  2. Two nested loops over ; nested loops →
  3. Halving the input each step
  4. Sequential (non-nested) blocks: take the highest order —
  5. Drop constant multipliers:

Worked examples:

Example 1 — what is the complexity of this code?

FOR i ← 1 TO n
    FOR j ← 1 TO n
        result ← result + arr[i] * arr[j]
    NEXT j
NEXT i

Two nested loops, each from 1 to : the body executes times → .

Example 2 — what is the complexity of this code?

FOR i ← 1 TO n
    OUTPUT arr[i]
NEXT i
FOR i ← 1 TO n
    total ← total + arr[i]
NEXT i

Two sequential loops each : total is .

How much of this have you taken in?

Quiz yourself on this section, free, no card needed.

Test myself

Comparing Algorithms Using Big-O

Worked comparison — searching algorithms:

AlgorithmBestAverageWorst
Linear search
Binary search
BST search

Worked comparison — sorting algorithms:

AlgorithmBestAverageWorstSpace
Bubble sort*
Merge sort

* best case with early-termination optimisation (see sorting algorithms lesson).

For large , an algorithm is always faster than an algorithm in the long run, regardless of constant factors.

Space Complexity

Big-O applies to space (memory) as well as time.

  • space — the algorithm uses a fixed amount of memory regardless of input size (e.g. bubble sort uses only a temp variable)
  • space — memory grows proportionally with input size (e.g. merge sort needs an auxiliary array of size )
  • space — adjacency matrix for a graph with vertices

Time and space complexity may trade off against each other. An algorithm with better time complexity often requires more memory (e.g. merge sort vs bubble sort).

Common Exam Mistakes

1. Retaining constant factors in Big-O

is written as . Big-O ignores constant multipliers. Writing is not wrong in the strictest sense, but is non-standard and would not earn full marks in an exam answer asking for Big-O class.

2. Giving best-case complexity when worst-case is asked

Exam questions typically ask about worst-case complexity. Bubble sort worst-case is , not (which is only the optimised best case).

3. Confusing with

Merge sort is ; bubble sort is . Since , these are very different classes for large .

4. Forgetting to take the dominant term

simplifies to . Only the fastest-growing term matters asymptotically.

Key terms

Big-O notation
Notation describing the asymptotic upper bound on the growth rate of a function as n approaches infinity, ignoring constant factors and lower-order terms.
Asymptotic
Describing behaviour of a function's growth rate as the input size n tends towards infinity.
Constant — O(1)
A complexity class where running time does not depend on input size, such as accessing an array element by index.
Logarithmic — O(log n)
A complexity class where the input size is halved or reduced by a constant factor each step, such as binary search.
Linear — O(n)
A complexity class with one operation per input element, such as a linear search reading every element once.
Polynomial — O(nᵏ)
A complexity class produced by k nested loops; two nested loops give O(n²) and three give O(n³).
Exponential — O(kⁿ)
A complexity class where the work multiplies with each unit increase in n, growing too fast to be practical beyond small inputs.
Factorial — O(n!)
A growth class following f(n) = n!, such as brute-force travelling salesman trying all permutations.
Time complexity
How an algorithm's running time grows as the input size increases.
Space complexity
How an algorithm's memory use grows with input size; O(1) is fixed memory and O(n) grows proportionally.

Frequently asked questions

Big-O notation compares algorithms by how their time or space requirements grow as input size increases, independent of hardware speed and specific constant factors. It describes the asymptotic upper bound on growth as n approaches infinity, ignoring constant factors and lower-order terms.

A single loop over n elements is O(n), two nested loops over n are O(n²) and k nested loops O(nᵏ), halving the input each step is O(log n). For sequential non-nested blocks take the highest order, and drop constant multipliers, so O(5n) = O(n).

Merge sort is O(n log n) and bubble sort is O(n²). Since log n is much smaller than n, an O(n log n) algorithm is always faster than an O(n²) one for large n, regardless of constant factors.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

BNF and Syntax Diagrams

Next

Limits of Computation and Tractability

Related lessons

5 min

6 min

Lesson

Sorting Algorithms

A-Level Computer Science · AQA 7517

1 month ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.