Intermediate

Complexity and Big-O Notation

AicademyAicademy
·A-Level Computer Science·AQA 7517·5 min
4.4.4.1 Comparing algorithms·4.4.4.2 A model for computational complexity·4.4.4.3 Order of complexity

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 :

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 .

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

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.

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

6 Slides

Lesson

Searching Algorithms

A-Level Computer Science · AQA 7517

10 hours ago

7 Slides

Lesson

Sorting Algorithms

A-Level Computer Science · AQA 7517

10 hours ago

7 Slides

Lesson

Limits of Computation and Tractability

A-Level Computer Science · AQA 7517

10 hours ago