19.1 Algorithms

Bulk view disabled for Guests. View lessons individually.

Big O Notation and Comparing Algorithms

1. Why Compare Algorithms At All?

Two algorithms can both solve the same problem correctly and still be wildly different in practice. Choosing between them needs measurable criteria, and the syllabus names two:

  • Time taken to complete the task — time complexity
  • Memory used while running — space complexity

Timing an algorithm with a stopwatch is unreliable: the result depends on the processor, the language, and what else the machine is doing. So instead we measure how the work grows as the amount of data grows. That measure is independent of any particular computer.

2. Big O Notation

Big O notation describes the order of growth of an algorithm — how the time or memory requirement scales with the number of data items, n.

Big O describes the worst case, and it deliberately ignores constants and small terms. An algorithm taking 3n + 7 steps is O(n), because as n becomes large only the n term matters.

3. The Orders You Need

Big ONameBehaviour as data doublesTypical example
O(1)ConstantNo change at allReading one array element by index; pushing to a stack
O(log n)LogarithmicAdds one extra stepBinary search
O(n)LinearTakes twice as longLinear search; totalling an array
O(n log n)LinearithmicSlightly more than doublesEfficient sorts such as merge sort
O(n²)QuadraticTakes four times as longBubble sort; insertion sort
O(2ⁿ)ExponentialBecomes unusable very quicklyBrute-force search of all combinations

What the growth actually looks like

nO(log n)O(n)O(n²)
10310100
100710010 000
1 000101 0001 000 000

This table is the whole argument for caring about complexity. At n = 1000 a logarithmic algorithm needs 10 steps and a quadratic one needs a million.

4. Working Out the Complexity

Count how many times the innermost work is done, in terms of n.

A single loop is O(n)

Total ← 0 FOR i ← 1 TO n Total ← Total + Items[i] // runs n times -> O(n) NEXT i

A nested loop is O(n²)

FOR i ← 1 TO n FOR j ← 1 TO n // runs n x n times -> O(n²) NEXT j NEXT i

Halving the data is O(log n)

WHILE Low <= High Mid ← (Low + High) DIV 2 // the search area halves each pass -> O(log n) ENDWHILE
Rule of thumb: one loop over the data is O(n); a loop inside a loop is O(n²); repeatedly halving is O(log n); no loop at all is O(1).

5. Complexity of the Required Algorithms

AlgorithmTime (worst case)Time (best case)Space
Linear searchO(n)O(1) — first itemO(1)
Binary searchO(log n)O(1) — middle itemO(1)
Bubble sortO(n²)O(n) — already sorted, with flagO(1)
Insertion sortO(n²)O(n) — already sortedO(1)
Both sorts are O(1) in space because they sort in place, needing only a single temporary variable regardless of how much data there is. An algorithm that copied the data into a second array would be O(n) in space.

6. The Time–Space Trade-off

Reducing time often costs memory, and reducing memory often costs time. A worked comparison:

ApproachTimeSpace
Search an unsorted array directlyO(n)O(1)
Build an index first, then search itO(log n) per searchO(n) for the index

Which is better depends on use: for a single lookup the direct search wins, because building an index costs more than it saves. For thousands of repeated lookups the index wins easily.

7. Exam Focus

Give the order, not a step count. The answer to "state the time complexity of a bubble sort" is O(n²). Writing "n × n comparisons" or "about 100 steps" does not earn the mark.
Drop constants and coefficients. O(2n), O(n + 5) and O(3n + 2) are all written O(n). Retaining the constant is a marked error.
Say which case you mean. Bubble sort is O(n²) in the worst case but O(n) in the best case if it uses a swapped flag. Questions about "best case" expect that distinction, and expect you to mention the flag.
Justify comparisons with the criteria named in the syllabus. When asked to compare two algorithms, argue in terms of time taken and memory used, and reference how each scales with the number of data items.

Quick self-check

  • Define time complexity and space complexity.
  • Give the Big O time complexity of linear search, binary search and bubble sort.
  • An algorithm takes 4n + 12 steps. State its Big O complexity.
  • Why are bubble sort and insertion sort O(1) in space?
  • If an O(n²) algorithm takes 2 seconds on 1 000 items, roughly how long on 2 000?
  • Explain one situation where using more memory produces a faster program.