The Order of an Algorithm: Counting Comparisons and Predicting Run Times | OCR A Level Further Maths notes
Free preview: The Order of an Algorithm: Counting Comparisons and Predicting Run Times
How Fast Does the Work Grow?
Year 1 (AS) for OCR B (MEI). For Edexcel and OCR A, check your own specification for which parts are AS and which are A level.
Let's start with a question every programmer asks: if my program sorts 1000 names in 1 second, how long will it take for a million names? 1000 seconds? Much longer? The answer depends on the shape of the growth, and that is what this note is about.
The words we need
- The size of a problem, n, is how much data it has: the number of items to sort, bins to fill or vertices in a network.
- The efficiency of an algorithm is how the amount of work grows as n grows.
- We measure work by counting the main operations, usually comparisons (and sometimes swaps or additions).
- We usually count the worst case: the input that makes the algorithm do the most work. Then we know it can never be slower than our count.
Order: keep only the biggest power
You met bubble sort in an earlier note. On n items it needs at most this many comparisons:
| n | Total comparisons | ||
|---|---|---|---|
| 10 | 50 | 5 | 45 |
| 100 | 5000 | 50 | 4950 |
| 1000 | 500 000 | 500 | 499 500 |
- When n is large, the term swamps the rest: for n = 1000 it is 500 000 against 500.
- Constants like depend on the computer and the details, so we ignore them too.
- So we say bubble sort has quadratic order, or order , written .
💡 In plain words
- The order tells you the shape of the growth, not the exact time.
- Order n (linear): double the data, double the time. Order (quadratic): double the data, four times the time. Order (cubic): eight times.

Following the graph
- All three curves start together at small n. The differences only matter for large problems.
- On the order curve, going from n = 2 to n = 4 (double) takes the steps from 4 to 16 (four times).
- The cubic curve shoots up fastest. A cubic algorithm can be useless for big n even on a fast computer.
- The linear curve grows steadily: it is the cheapest of the three.
✏️ YOUR TURN 1
Try this first:
State the order of an algorithm that needs this many operations in the worst case.
Answer:
- Highest power : quadratic, . The 3 is a constant, so it is ignored.
- Highest power n: linear, .
- Expanding gives : cubic, . You only need to see that three brackets each containing n multiply to make .
Finding the Order by Counting
The recipe
- Decide what counts as one operation (usually a comparison). Why: we need one thing to count, and comparisons are where the time goes.
- Find the worst case: the input that makes the algorithm do the most work. Why: then the order is a guarantee, not a hope.
- Write the number of operations as an expression in n, often a sum such as . Why: a sum is easier to write down than to see in your head.
- Simplify, then keep the highest power of n. Why: that is the term that matters for big n.
💡 The sum you'll use most
. This is the arithmetic series formula from A Level Maths: terms, average value .
Four examples
- Finding the largest of n numbers by checking each against the biggest so far: comparisons. Order n.
- First fit bin packing: the kth item may be checked against each of the bins already open. Worst case . Order .
- First fit decreasing: a bubble sort (order ) then first fit (order ). Adding two quadratics gives a quadratic, so order .
- Comparing every pair from a list of n items (for i = 1 to n, for j = 1 to n): comparisons, order . A loop inside a loop usually means quadratic.
Unlock the full note: start 7 days free.
Log in or subscribe to read the rest of this note. Members also get the AI tutor, practice quizzes, mock exams and flashcards for this topic.
All notes in Algorithms, Complexity, Sorting and Bin Packing
- What Is an Algorithm? Flow Charts, Pseudocode and Trace Tables
- Bubble Sort, Shuttle Sort and Quick Sort Step by Step
- Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
- The Order of an Algorithm: Counting Comparisons and Predicting Run Times
What's inside for this topic
4 teacher notes, 80 practice questions, 52 flashcards, 2 quizzes.
Get the full notes, every practice question, flashcards, quizzes and mock papers.
Previous note: Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
All OCR A Level Further Maths topics · A Level Further Maths