B2.4.1 · Programming algorithms
Algorithm efficiency & Big O
By the end you can
Analyze best, average, and worst-case time and space complexity across O(1), O(log n), O(n), and O(n²).
Two different algorithms may both produce the correct result, but one might complete in milliseconds while the other takes hours. measures the computational resources (execution time and memory space) consumed by an algorithm as input size $n$ grows.
Big O complexity classes#
We use to categorize algorithms by their worst-case asymptotic growth rate:
- $O(1)$ : Constant time: Execution time does not depend on dataset size. Examples include reading an array index (
arr[i]), pushing onto a stack, or simple arithmetic. - $O(\log n)$ : Logarithmic time: The problem space is divided by two on each step. Highly efficient even for billions of records. Example: Binary Search.
- $O(n)$ : Linear time: Operations scale in direct proportion to input size. Example: Linear search iterating through an unsorted list.
- $O(n \log n)$ : Linearithmic time: The fastest possible comparison-based sorting algorithms (Merge Sort, Quick Sort).
- $O(n^2)$ : Quadratic time: Operations scale with the square of input size, typical of nested loops comparing every pair of elements. Examples: Bubble Sort, Selection Sort.
- $O(2^n)$ : Exponential time: Operations double with each added item. Quickly becomes intractable for large inputs.
Time complexity vs Space complexity#
Time complexity tracks how the number of instructions executed scales with $n$.Space complexity tracks the extra working RAM (excluding initial input storage) required during execution. For example, Bubble sort uses $O(1)$ auxiliary space because it swaps elements in place, whereas Merge sort requires $O(n)$ extra memory to hold temporary halves.
Check yourself
Big O notation
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 8 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Define the term Big O notation and explain why it is used to evaluate algorithm performance.
[2]Define: Give the precise meaning of the term.
2.Identify the Big O time complexity for each of the following: (a) Accessing an array element by index, (b) Binary search in a sorted array, (c) Linear search in an array, (d) Bubble sort worst case.
[4]Identify: Pick out the answer from the options or the context.
3.Distinguish between time complexity and space complexity of an algorithm.
[2]Distinguish: Make the differences between two things clear.
In a nutshell
- Algorithm efficiency measures computational time and memory resources consumed as input size n approaches infinity.
- Big O notation represents the worst-case asymptotic growth rate independently of hardware specs.
- Common classes: O(1) constant, O(log n) logarithmic, O(n) linear, O(n log n) linearithmic, and O(n²) quadratic.
- Time complexity measures instruction operations; space complexity measures auxiliary working RAM.
