Pro Content

Upgrade to access all Cambridge 9618 lessons including Big-O notation and algorithm complexity analysis.

Upgrade to Pro →
← Back to Dashboard
🔢 Paper 2 · 2.4 Algorithms
2.4.5 Big-O Notation & Algorithm Efficiency
Cambridge 9618 · International A Level Computer Science · ~15 min read
Notes
Video
Slides
Quiz
Worksheet

What is Big-O Notation?

Big-O notation describes how an algorithm's time requirements (or memory usage) grow as the input size n increases. It focuses on the dominant term and worst-case behaviour — ignoring constants and lower-order terms because for large n they become irrelevant.

Big-O answers the question: "If I double the input size, how much longer does this algorithm take?"

Key Terms

  • n — the size of the input (e.g. number of elements in an array)
  • Time complexity — how the number of operations grows with n
  • Space complexity — how the memory usage grows with n
  • Worst case — the maximum number of operations for any input of size n
  • Best case — the minimum number of operations (e.g. the item is found first)
  • Average case — typical performance averaged over all possible inputs

The Six Common Complexities

O(1)
Constant
Performance does not change with n. Always the same number of operations regardless of input size.
Example: accessing Stack[Top], array indexing arr[5], checking isEmpty()
O(log n)
Logarithmic
Operations grow very slowly. Each step halves the remaining problem.
Example: Binary search — each comparison eliminates half the array
O(n)
Linear
Operations grow proportionally to n. Double the input → double the operations.
Example: Linear search, traversing a linked list, reading n file records
O(n log n)
Linearithmic
Slightly worse than linear. Best achievable complexity for comparison-based sorting.
Example: Merge sort (and other efficient sorts like quicksort average case)
O(n²)
Quadratic
Operations grow with the square of n. Double input → 4× longer. Nested loops.
Example: Bubble sort, insertion sort (worst case), selection sort
O(2ⁿ)
Exponential
Operations double for each +1 in n. Completely impractical for large inputs.
Example: Recursive Fibonacci (without memoisation), brute-force subset generation

Operations for Different Input Sizes

How many operations does each complexity class perform for typical values of n?

nO(1)O(log n)O(n)O(n log n)O(n²)O(2ⁿ)
101310331001,024
1001710066410,0001.27×10³⁰
1,0001101,0009,9661,000,000∞ (impossible)
1,000,0001201,000,00019,931,56810¹²

From Best to Worst: Efficiency Order

O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ)

Left = most efficient   |   Right = least efficient

Big-O for Algorithms You've Studied

AlgorithmBest caseAverage caseWorst caseSpace
Linear SearchO(1)O(n)O(n)O(1)
Binary SearchO(1)O(log n)O(log n)O(1)
Bubble SortO(n)*O(n²)O(n²)O(1)
Insertion SortO(n)O(n²)O(n²)O(1)
Merge SortO(n log n)O(n log n)O(n log n)O(n)
Stack Push/PopO(1)O(1)O(1)O(1)
Factorial (recursive)O(n)O(n)O(n)O(n) stack
Fibonacci (recursive)O(2ⁿ)O(2ⁿ)O(2ⁿ)O(n) stack

*Bubble sort O(n) best case requires the early exit (swapped flag) optimisation.

How to Identify Big-O from Code

  • Single operation, no loop: O(1)
  • One loop over n items: O(n)
  • Two nested loops over n items: O(n²)
  • Problem halved each step: O(log n) — e.g. binary search
  • Loop over n + halving: O(n log n) — e.g. merge sort
  • Recursive with two calls on n-1 and n-2: O(2ⁿ) — e.g. Fibonacci
Cambridge exam — state the complexity: Always express complexity in standard form (e.g. "O(n²)", not "n squared" or "quadratic"). For bubble sort, specify whether you mean worst or best case. If asked "which algorithm is more efficient?", always justify your answer by comparing complexities.
⚠️ Common Mistakes
  • Saying bubble sort is always O(n) — it's O(n) only in the best case with early exit; worst/average is O(n²)
  • Confusing O(log n) with O(n log n) — binary search is O(log n); merge sort is O(n log n)
  • Saying merge sort has O(n²) — it's always O(n log n) regardless of input order
  • Forgetting space complexity — merge sort is O(n) space; bubble/insertion sort are O(1) in-place
  • Writing O(n) + O(n) = O(2n) — Big-O ignores constants, so O(2n) simplifies to O(n)
  • Confusing best case and worst case — linear search: best O(1) (found first), worst O(n) (not found)
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 2.4.5 Big-O Notation

8 questions · Cambridge 9618 standard

Q1State what Big-O notation measures and explain why constants are ignored in Big-O analysis.[3]
✅ Mark scheme
Big-O measures how the time (or space) requirements of an algorithm grow as the input size n increases [1]; it describes the worst-case growth rate [1]; constants are ignored because for large n, constants become insignificant compared to the dominant growth term — e.g. 100n is still O(n) because as n grows, the constant 100 has negligible effect relative to n [1].
Q2For each algorithm, state its worst-case time complexity: (a) Linear search, (b) Binary search, (c) Bubble sort, (d) Merge sort.[4]
✅ Mark scheme
(a) Linear search: O(n) [1]; (b) Binary search: O(log₂ n) [1]; (c) Bubble sort: O(n²) [1]; (d) Merge sort: O(n log n) [1].
Q3An algorithm contains two nested FOR loops, each running from 1 to n. State the time complexity and justify your answer.[2]
✅ Mark scheme
O(n²) [1]; the inner loop runs n times for each of the n iterations of the outer loop, giving n×n = n² operations total [1].
Q4A dataset has 1,000,000 records. Compare the approximate number of operations for: (a) linear search, (b) binary search to find a specific record. Why is binary search preferred?[3]
✅ Mark scheme
(a) Linear search worst case: O(n) → up to 1,000,000 comparisons [1]; (b) Binary search worst case: O(log₂ n) → log₂(1,000,000) ≈ 20 comparisons [1]; binary search is far more efficient for large sorted datasets — 20 comparisons vs 1,000,000 comparisons [1].
Q5Order these complexities from most efficient to least efficient: O(n²), O(1), O(n log n), O(log n), O(n), O(2ⁿ).[2]
✅ Mark scheme
O(1) → O(log n) → O(n) → O(n log n) → O(n²) → O(2ⁿ) [2 marks — 1 mark if mostly correct with no more than one error].
Q6Explain why merge sort (O(n log n)) is preferred over bubble sort (O(n²)) for sorting 100,000 records, even though merge sort uses more memory.[3]
✅ Mark scheme
For n=100,000: bubble sort ≈ 10¹⁰ operations; merge sort ≈ 100,000 × 17 ≈ 1.7 million operations [1]; the O(n log n) vs O(n²) difference is enormous at large n — merge sort is orders of magnitude faster [1]; the extra O(n) memory for merge sort's temporary arrays is a reasonable trade-off for the massive speed improvement when n is large [1].
Q7Write pseudocode for a procedure that opens a file "log.txt", attempts to read a line, and handles two possible errors: (1) the file does not exist, and (2) the file is empty. Use TRY-EXCEPT-FINALLY structure and explain the role of FINALLY.[5]
✅ Mark scheme
TRY block: OPENFILE "log.txt" FOR READ; READFILE "log.txt", line — 1 mark; EXCEPT FileNotFoundException: OUTPUT "File not found" — 1 mark; EXCEPT EmptyFileException: OUTPUT "File is empty" — 1 mark; FINALLY: CLOSEFILE "log.txt" (if open) — 1 mark; FINALLY always executes regardless of exception — ensures resource cleanup — 1 mark.
Q8Explain the difference between a syntax error, a runtime error, and a logic error. For each type, give one example that could occur in a file-handling program and explain how a programmer would detect and fix it.[6]
✅ Mark scheme
Syntax: code that violates language rules, caught at compile time — 1 mark; e.g. OPNFILE instead of OPENFILE — detected by compiler/interpreter — 1 mark; Runtime: occurs during execution, e.g. attempting to read a file that does not exist — 1 mark; detected at run time, handled with TRY-EXCEPT — 1 mark; Logic: program runs but produces wrong output, e.g. reading from wrong file name — 1 mark; detected by testing with known data and comparing output — 1 mark.
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 7
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 2.4.5 Big-O Notation

10 questions · 10 marks · 10 minutes

← 2.4.4 Recursion
47 of 82 · Cambridge 9618
2.5.1 Programming Paradigms →