All subjects · Computer Science · WCP03 Advanced Principles of Computer Science

Algorithmic efficiency and Big O notation

Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science

Big O notation describes how an algorithm's running time grows as the input grows. It lets you compare algorithms without timing them on a particular computer, and it explains why some problems become impossible at scale.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

What Big O measures

Big O describes the upper bound on the growth rate of time (or memory) as the input size n increases. It keeps only the dominant term and ignores constants: 3n2 + 5n + 20 is O(n2), because for large n the n2 term swamps the rest.

It is needed to: compare algorithms independently of hardware; predict how a program will cope with much more data; choose a suitable algorithm; spot intractable problems.

The complexity classes

From slowest-growing to fastest-growing. The last column shows how many steps each needs for 1000 items.

ClassNameExamplen = 1000, roughly
O(1)ConstantAccessing an array element by index; hash table lookup (average)1
O(log n)LogarithmicBinary search; search in a balanced BST10
O(n)LinearLinear search; traversing a linked list1000
O(n log n)LinearithmicMerge sort; quick sort (average)9966
O(n2)Polynomial (quadratic)Bubble sort; insertion sort (worst); nested loops over the data1 000 000
O(2n)ExponentialBrute force over every subset; naive recursive Fibonacciabout 10301

Polynomial also includes n3 and higher powers

Best and worst cases

Exam questions often ask for both. The worst case is what Big O usually quotes.

AlgorithmBest caseWorst case
Linear searchO(1): target is firstO(n): target last or absent
Binary search (sorted data)O(1): target in the middleO(log n)
Bubble sort (with early stop)O(n): already sortedO(n2)
Insertion sortO(n): already sortedO(n2)
Merge sortO(n log n)O(n log n)
Quick sortO(n log n)O(n2): extreme pivots
Hash table searchO(1)O(n): many collisions
BST searchO(1): target at rootO(n): unbalanced chain

Finding the Big O of code

for i in range(n):          # n times
    for j in range(n):      # n times each
        total += i * j      # O(1)
# O(n^2)

while n > 1:                # halves each time
    n = n // 2
# O(log n)

Worked example

A sorting algorithm takes 2 seconds for 1000 items and is O(n2). Estimate the time for 3000 items.

  1. n is multiplied by 3.
  2. For O(n2), time is multiplied by 32 = 9.
  3. 2 x 9 = 18 seconds.

Answer: About 18 seconds

Key idea: Big O keeps the dominant term: O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(2n). Nested loops multiply; halving gives log n. Know best and worst cases of the standard algorithms.

Check yourself

  1. What is the Big O of an algorithm whose number of steps is 4n2 + 10n + 7?
    • O(4n2)
    • O(n)
    • O(n2)
    • O(n2 + n)
    Show answer

    O(n2). Keep the dominant term and drop constants.

  2. What is the worst-case time complexity of binary search?
    • O(log n)
    • O(n)
    • O(1)
    • O(n log n)
    Show answer

    O(log n). Each comparison halves the remaining items.

  3. What is the time complexity of a loop over n items nested inside another loop over n items, with O(1) work inside?
    • O(2n)
    • O(n log n)
    • O(n2)
    • O(log n)
    Show answer

    O(n2). n x n iterations.

  4. What is the time complexity of: while n > 1: n = n // 2?
    • O(n)
    • O(log n)
    • O(n2)
    • O(1)
    Show answer

    O(log n). The number of passes is about log2 n.

  5. What is the maximum number of comparisons binary search needs for a sorted list of 1000 items?
    Show answer

    10. 29 = 512 < 1000 <= 1023 = 210 - 1, so at most 10 comparisons.

  6. An O(n2) algorithm takes 5 ms for 200 items. Estimate the time for 800 items, in ms.
    Show answer

    80 ms. n x 4, so time x 16: 5 x 16 = 80 ms.

  7. Merge sort makes about n log2 n comparisons. Estimate this for n = 1024.
    Show answer

    10240. 1024 x 10 = 10 240.

  8. Which statement describes linear time, O(n)?
    • The running time stays the same however many items there are
    • The running time doubles with each extra item
    • The running time grows in direct proportion to the number of items
    • The running time grows with the square of the number of items
    Show answer

    The running time grows in direct proportion to the number of items. Double the data, double the time.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.