Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do we measure and compare the efficiency of algorithms independently of the machine they run on?

Analyse the time and space complexity of an algorithm using Big-O notation, and compare common growth rates

A focused answer to the H2 Computing outcome on algorithmic complexity. Big-O notation, deriving the order of growth from loops and recursion, the common complexity classes, and the trade-off between time and space.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to analyse how an algorithm's running time and memory use grow with the input size, expressed in Big-O notation, and to compare the common growth rates. The central idea is that Big-O describes the order of growth as the input gets large, ignoring machine speed and constant factors, so two algorithms can be compared independently of the hardware.

The answer

What Big-O measures

Big-O notation describes an upper bound on how the cost of an algorithm grows as the input size nn grows. It captures the dominant term and drops constants and lower-order terms, because for large nn those do not matter:

3n2+5n+9O(n2)3n^2 + 5n + 9 \in O(n^2)

We care about nn getting large because that is where efficiency differences become decisive.

Reading complexity off code

Count how the number of basic operations scales with nn:

  • A single loop over nn items is O(n)O(n).
  • A loop nested inside another, each over nn, is O(n2)O(n^2).
  • Halving the problem each step gives O(logn)O(\log n).
  • Splitting into halves and doing linear work to combine gives O(nlogn)O(n \log n).
# O(n): one pass
for x in data:
    process(x)

# O(n^2): nested passes
for i in data:
    for j in data:
        compare(i, j)

The common complexity classes

From fastest-growing (worst) to slowest-growing (best) for large nn:

O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(2n)O(1) < O(\log n) < O(n) < O(n \log n) < O(n^2) < O(2^n)

  • O(1)O(1) constant - independent of nn.
  • O(logn)O(\log n) logarithmic - halving each step (binary search).
  • O(n)O(n) linear - one pass.
  • O(nlogn)O(n \log n) - good comparison sorts (merge sort).
  • O(n2)O(n^2) quadratic - nested loops (bubble sort).
  • O(2n)O(2^n) exponential - infeasible beyond small nn.

Time versus space

Complexity applies to time (operations) and space (extra memory). They often trade off: an algorithm can run faster by using more memory (for example storing precomputed results), or use less memory by recomputing and running slower.

Examples in context

Example 1. Why search engines do not scan everything. A linear scan of billions of web pages per query would be O(n)O(n) in the size of the index - far too slow. Search engines build indexes that allow near-O(logn)O(\log n) or constant-time lookups, which is the whole reason results return in milliseconds.

Example 2. Choosing a sort in practice. A teaching example might use bubble sort (O(n2)O(n^2)) for clarity, but a library sorting a million records uses an O(nlogn)O(n \log n) algorithm. The difference is not academic: at n=106n = 10^6, the quadratic version does roughly a trillion operations versus about twenty million for nlognn \log n.

Try this

Q1. State the Big-O time complexity of summing all elements of a list of nn numbers. [1 mark]

  • Cue. O(n)O(n) - one pass adds each element once.

Q2. Reduce O(4n2+2n+7)O(4n^2 + 2n + 7) to standard Big-O form. [1 mark]

  • Cue. O(n2)O(n^2) - keep only the dominant term, drop constants and lower-order terms.

Q3. An algorithm halves its input each step. State and justify its time complexity. [2 marks]

  • Cue. O(logn)O(\log n) - the number of steps is the number of times nn can be halved, which is log2n\log_2 n.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marksState the Big-O time complexity of each of the following and justify briefly: (a) finding the maximum of an unsorted list of nn items; (b) a nested loop where the outer runs nn times and the inner runs nn times; (c) binary search on a sorted list of nn items.
Show worked answer →

(a) O(n)O(n). You must inspect every element once to be sure of the maximum, so the work grows linearly with nn.

(b) O(n2)O(n^2). The inner loop runs nn times for each of the nn outer iterations, giving ntimesn=n2n \\times n = n^2 basic operations.

(c) O(logn)O(\\log n). Binary search halves the search range each step, so the number of steps is the number of times nn can be halved, which is log2n\\log_2 n.

Markers reward each correct class with a one-line justification: linear because every item is seen once, quadratic because of the nested loop, logarithmic because the range halves.

Original5 marksTwo algorithms solve the same problem: Algorithm A runs in O(n2)O(n^2) time using O(1)O(1) extra space; Algorithm B runs in O(nlogn)O(n \log n) time using O(n)O(n) extra space. (a) Which is faster for very large nn, and why? (b) Give a situation where you might still prefer Algorithm A. (c) Explain what O(1)O(1) space means.
Show worked answer →

(a) Algorithm B is faster for very large nn, because nlognn \\log n grows much more slowly than n2n^2; as nn increases the quadratic term dominates and A's running time pulls far ahead of B's.

(b) You might prefer A when memory is very tight or nn is small. A uses only constant extra space, so on a device with little memory, or for small inputs where the constant factors make A competitive, A can be the better choice.

(c) O(1)O(1) space means the extra memory used does not grow with the input size - the algorithm needs only a fixed number of variables regardless of how large nn is.

Markers reward identifying B as asymptotically faster with the growth-rate reason, a valid trade-off favouring A (low memory or small n), and a correct definition of constant space.

Related dot points