Big-O Notation & Algorithm Efficiency
Flip between the growth curves and see why an O(n²) algorithm crawls where an O(n log n) one flies. Big-O notation, made visual.
Two programs solve the same problem. One is fine on a million records; the other grinds to a halt on a thousand. Big-O notationis the language that captures that difference — not how fast an algorithm is, but how well it scales.
What Big-O measures
Big-O describes how the amount of work an algorithm does grows as the size of its input, n, grows. It deliberately throws away detail that does not matter for large inputs: constant factors (is each step twice as fast?) and lower-order terms. What is left is the shape of the growth — the thing that decides whether an algorithm survives a big dataset.
So Big-O is not measured in seconds. It answers a sharper question: if I double the input, roughly how much more work is there? An O(n) algorithm does twice as much; an O(n²) algorithm does four times as much.
Big-O is about the slope of the growth, not the height of the curve at any one point.
See it: compare growth rates
The differences between complexity classes are invisible for tiny inputs and overwhelming for large ones. The graph below plots operations against input size n for each class at once, so you can watch the curves fan apart.
Select a class to draw it boldly, and drag the point to read the operations at a chosen n.
Text description ↓Hide text description ↑
A graph of operations against input size n for the common Big-O classes. Selecting a class draws it boldly with the others faint behind it, so you can see O(1) stay flat, O(log n) barely rise, O(n) climb steadily, O(n log n) a little faster, and O(n²) and O(2ⁿ) shoot upwards. Dragging the point reads off the number of operations at a given n.
The common complexity classes
A handful of classes cover almost everything you will meet, listed here from best to worst:
- O(1) — constant. The work is fixed, whatever the input size. Reading array element
a[i]by its index takes the same time in a list of ten or ten million. - O(log n) — logarithmic. Each step throws away half the remaining data. A binary search of a sorted list finds any item in a handful of steps.
- O(n) — linear. The work is proportional to the input. A linear search that checks every item once is the classic example.
- O(n log n) — linearithmic. A little steeper than linear. Merge sort and the other efficient sorts live here.
- O(n²) — quadratic. Work grows with the square of n. Two nested loops over the same data, or bubble sort, give this.
- O(2ⁿ) — exponential. Work doubles with each extra input. Some brute-force approaches (trying every subset of n items) blow up like this and become unusable even for modest n.
Working out an algorithm’s Big-O
You rarely need to count operations exactly. Three rules get you the class quickly.
Nested loops multiply. A loop over n inside another loop over n runs n × n times — O(n²). Consecutive stages add: a loop over n followed by a separate loop over n is O(n) + O(n), which is still O(n). Finally, Big-O usually describes the worst case— the most work the algorithm could be forced to do — because that is the guarantee that matters.
Common mistakes
Practice
An algorithm runs two separate loops over the n items, one after the other, and then a nested double loop over the same n items. What is its overall Big-O?
Show the answer ↓Hide the answer ↑
The two single loops are O(n) + O(n) = O(2n), which simplifies to O(n). The nested double loop is O(n × n) = O(n²). Running the stages in sequence, you add them: O(n) + O(n²). The fastest-growing term wins, so overall it is O(n²).
Where next?
Big-O comes to life when you watch it happen: the sorting algorithms guide runs O(n²) and O(n log n) methods side by side with live operation counts, so you can see these curves play out on a real list.
Frequently asked questions
What does Big-O notation mean?+
Why do we ignore constants in Big-O?+
Which is better, O(n log n) or O(n²)?+
What is the worst time complexity?+
The ScholarsGate Computer Science Team
Oxbridge & Russell Group computer science tutors
Written and reviewed by ScholarsGate tutors who teach GCSE, A-Level and undergraduate computer science. Every explainer is checked against the AQA and OCR specifications.
Keep exploring
Want a tutor to walk you through it?
Book a DBS-checked A-Level Computer Science tutor for a 1-on-1 lesson — online or in person.
Find a A-Level Computer Science tutor