A Level · Topic 19.1
A-Level Computer Science 9618 / Topic 19 / A2

Count the growth.

Two algorithms that do the same job can be compared by the time they take and the memory they use. Big O notation describes how each of these grows as the amount of data grows, so you can choose the right algorithm before writing a line of code.

The key idea

Algorithms that perform the same task are compared by the time taken (the number of basic steps, such as comparisons) and the memory used, as the number of data items n grows. Big O notation gives the order of growth in the worst case: keep only the dominant term and drop constants, so 3n2 + 5n + 2 steps is O(n2). Time complexity ranks O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(2n). Space complexity uses the same notation for extra memory: an in-place sort is O(1), a copy of the data is O(n).

How the number of steps grows as the number of items n grows steps (time) number of data items, n O(1) O(log n) O(n) O(n log n) O(n²) O(2ⁿ)
Fig. 1: the common orders of growth. For small n they are close; as n grows, O(n2) and O(2n) quickly become unusable.
Section 01

Fair comparisons.

Timing two programs with a stopwatch is not a fair test: the result depends on the processor, the language, the compiler and whatever else is running. Instead, count the basic operations the algorithm performs as a function of n, the number of data items.

Count ← 0
FOR i ← 1 TO n
    FOR j ← 1 TO n
        Count ← Count + 1
    NEXT j
NEXT i
Worked example Count the steps
How many times is Count ← Count + 1 executed by the code above when n = 10? What if the inner loop is changed to FOR j ← i TO n?
  1. Original: the inner loop runs n times for each of the n values of i, so n × n = 10 × 10 = 100.
  2. Changed: when i = 1 the inner loop runs 10 times, when i = 2 it runs 9 times, ..., when i = 10 it runs once.
  3. 10 + 9 + ... + 1 = 10 × 11 / 2 = 55, which is n(n + 1)/2 = 0.5n2 + 0.5n.
Answer: 100 and 55. Both grow with n2, so both are O(n2): halving the work does not change the order of growth
Your turn
A loop runs FOR i ← 1 TO n and its body contains three assignments and no other loop. How many assignments are executed for n = 50, and what is its time complexity?
Examiner trap

"Algorithm A is faster because it took 0.3 seconds" earns nothing. Compare the number of operations as n increases, or the Big O order.

Section 02

Big O notation.

In the lab, compare the two searches, then the two sorts, as the list grows. Switch the sorts to data that is already sorted and watch both curves flatten from n2 to n: the best case and the worst case have different orders.

OrderNameExampleIf n doubles, the time...
O(1)constantpush or pop on a stack; access List[i]stays the same
O(log n)logarithmicbinary search; search a balanced binary treegrows by one step
O(n)linearlinear search; find an item in a linked listdoubles
O(n log n)linearithmicefficient sorts such as merge sortslightly more than doubles
O(n2)polynomial (quadratic)bubble sort, insertion sort; nested loops over the datais multiplied by 4
O(2n)exponentialnaive recursive Fibonacci; trying every subsetdoubles each time n rises by just 1
Worked example Predict the run time
An O(n2) sort takes 2 seconds for 1000 items. Estimate its time for 3000 items. How would an O(n) algorithm that also takes 2 seconds for 1000 items compare?
  1. 3000 is 3 times 1000.
  2. O(n2): time is multiplied by 32 = 9, so 2 × 9 = 18 seconds.
  3. O(n): time is multiplied by 3, so 2 × 3 = 6 seconds.
Answer: About 18 s for the O(n2) sort against about 6 s for the O(n) algorithm
Your turn
Give the Big O time complexity of an algorithm that makes 4n + 7 log2 n + 20 comparisons, and of one that makes 0.01n3 + 500n comparisons.
Examiner trap

O(log n) does not mean "fewer than n" for every n, and O(n2) does not give an exact count. Big O describes how the work grows; for very small n a "worse" algorithm can still be quicker.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

In the lab, how does the binary search curve change as the list gets longer?

Quick check+10 XP

What is the time complexity of an algorithm that makes 5n2 + 100n + 3 comparisons?

Quick check+10 XP

Why is the number of operations a better measure than the time in seconds?

Quick check+10 XP

An insertion sort of n items uses only Key, i and j as extra variables. What is its space complexity?

Section 03

Space complexity.

Memory is the second criterion. Big O is used in the same way: how does the extra memory an algorithm needs grow as n grows? Often you can trade one resource for the other.

AlgorithmTime (worst case)Extra space
Linear searchO(n)O(1)
Binary search (iterative)O(log n)O(1)
Bubble sort, insertion sortO(n2)O(1): in place
Find in a binary search treeO(log n) if balanced, O(n) if notO(1) iterative
Push, pop, enqueue, dequeueO(1)O(1)
Recursive factorial of nO(n)O(n): n stack frames
Worked example Choose an algorithm
A device with very little RAM must sort 200 sensor readings once an hour. Algorithm P is O(n log n) time but needs a second array of n items. Algorithm Q is an insertion sort. Which should be chosen?
  1. Time: for n = 200, n2 = 40 000 against n log2 n of about 1529. Q does more work, but 40 000 steps once an hour is trivial for a processor.
  2. Space: P needs O(n) extra memory for the second array; Q sorts in place in O(1) extra memory.
  3. The binding constraint here is memory, not time.
Answer: Choose Q, the insertion sort: its O(1) space suits the small RAM, and its O(n2) time is acceptable for 200 items once an hour
Your turn
Why is a binary search tree built from data that arrives in sorted order a poor choice for searching?
Examiner trap

Do not confuse the two criteria. "It uses less memory" is a space argument; "it makes fewer comparisons" is a time argument. Say which one you mean and link it to n.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

An O(n2) algorithm takes 5 seconds for 2000 items. About how long will it take for 4000 items?

Exam style+20 XP

Which list is in order from slowest-growing to fastest-growing?

Exam style+20 XP

What is the worst-case time complexity of finding an item in a linked list of n nodes?

Exam style+20 XP

Which algorithm has exponential time complexity?

Skill unlocked

Big O notation, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Comparing algorithms by time and memory, and using Big O notation for time and space complexity.