B2.4.1 · Programming algorithms
Big O notation: measuring how an algorithm scales
Two algorithms can both give the right answer. Big O notation is how you tell which one is going to fall over first once the input gets big.
Explainer
Ravensbourne High's photocopier prints one handout per student. Printing for a class of 30 takes about 30 times as long as printing for 1 student. Print for two full classes (60 students) and it takes about twice as long as one class. That relationship, the work growing in step with how much there is to do, is exactly what Big O time complexity captures.
Big O notation describes how the number of steps an algorithm takes grows as the input size, usually written n, grows. It doesn't measure exact seconds, which change depending on the computer, it measures the shape of the growth.
Time complexity describes how the number of steps grows with input size. Space complexity describes how the amount of memory used grows with input size. Both are written using the same Big O notation, they just measure different things.
Explainer
Counting a piece of code's own steps
To find an algorithm's Big O, count how many times its slowest-growing line actually runs, for an input of size n, in the worst case.
One loop over n items runs n times: O(n).
A loop inside another loop, both over n, runs n × n times: O(n²).
No loop at all, one fixed number of steps regardless of n: O(1).
Explainer
Dropping constants and lower-order terms
Real algorithms rarely count out to a clean n or n², they usually come out as a full expression, like 3n² + 40n + 12. Big O only keeps the part that matters once n gets large.
Keep only the fastest-growing term, and drop its multiplier. 3n² + 40n + 12 becomes O(n²). The 40n and the 12 shrink to nothing compared to n² once n is large, and the 3 doesn't change which term grows fastest, only how big the steps are.
Explainer
The common growth rates, slowest to fastest
| Notation | Name | What it means |
|---|---|---|
O(1) | Constant | Same number of steps no matter how big n gets. |
O(log n) | Logarithmic | Steps grow very slowly, each doubling of n only adds roughly one more step. |
O(n) | Linear | Steps grow in direct proportion to n. |
O(n²) | Quadratic | Steps grow with the square of n, common with one loop nested inside another. |
O(2ⁿ) | Exponential | Steps roughly double every time n increases by one, becomes unusable very quickly. |
This is the order they're usually compared in, from best-scaling to worst-scaling, as n grows large.
Explainer
Space complexity: the same idea, applied to memory
Space complexity asks a different question about the same code: how much memory does it need, as n grows?
total_printed = 0 for student in range(n): total_printed = total_printed + 1
total_printed is one variable, overwritten every pass. It takes the same memory whether n is 5 or 5000.
printed_names = [] for student in range(n): printed_names.append(student)
printed_names grows by one entry every pass. Store all n students, and the memory used grows right along with n.
Explainer
Choosing an algorithm by scalability and efficiency
The "best" algorithm depends on what it's actually going to be used for, not on which one sounds cleverest.
Small, fixed inputs: if Ravensbourne's photocopier room only ever handles one class (about 30 students) at a time, an O(n²) approach is often perfectly fine, n never gets large enough for the difference to matter.
Large or growing inputs: if the same system had to handle the whole school (2000 students) printing at once, an O(n²) approach would slow down dramatically, an O(n) or O(n log n) approach scales far better.
Real-time requirements: a system that has to respond instantly, regardless of how much data it holds, needs O(1) or O(log n), not something that gets slower as more data piles up.
Worked example
Three photocopier jobs, three growth rates
The same three code shapes from the explainer, counted out for a real, small class of 4 students, to see the actual numbers before trusting the notation.
Step 1. One loop, O(n). 4 students, one handout each: 4 steps.
Step 2. Nested loop, O(n²). Every one of the 4 students compared against every other: 4 × 4 = 16 steps.
Step 3. No loop, O(1). Exactly 1 step, whether n is 4 or 4000.
Try it yourself
Watch O(n²) growth for real
The same nested-loop counter run for n = 5, 10 and 20. Change the values in the list and press Run to see whether the counts still roughly match n × n.
Running in offline fallback mode. The full Python engine couldn't load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It supports this page's loops and functions, but it isn't full Python.
Worked example
Simplifying a real expression
An algorithm's real step count comes out as 3n² + 40n + 12. Substituting real numbers shows exactly why the smaller terms stop mattering.
| n | 3n² | 40n | +12 | Total |
|---|---|---|---|---|
| 10 | 300 | 400 | 12 | 712 |
| 100 | 30,000 | 4,000 | 12 | 34,012 |
| 10,000 | 300,000,000 | 400,000 | 12 | 300,400,012 |
At n = 10, the 40n term (400) is actually bigger than the 3n² term (300). But by n = 10,000, 3n² is 300 million while 40n is only 400,000, barely a rounding error by comparison. The 3n² term is what decides how the total behaves once n is genuinely large, which is exactly why Big O keeps only that term: O(n²).
Check your understanding
An algorithm performs 5n² + 100n + 4 operations for an input of size n. What is its Big O time complexity?
Common mistake
RecurringAfter correctly counting an algorithm's operations, a common error is writing the full, unsimplified expression as the final Big O answer, or dropping the wrong term entirely, keeping a smaller term instead of the one that actually dominates.
Wrong, not simplified: "The time complexity is O(5n² + 100n + 4)." Counted correctly, but never reduced to the one term that actually decides the growth.
Wrong, dropped the wrong term: "The time complexity is O(100n), since 100 is the largest number." Confuses the size of a coefficient with which power of n grows fastest. n² will always overtake 100n eventually, no matter how large the 100 is.
Right: "The time complexity is O(n²), since n² grows faster than n and eventually dominates every other term, regardless of the constants in front of them."
Linking question
How this connects to the rest of the syllabus
The IB syllabus is deliberately woven together, not a stack of isolated topics. Here's how Big O connects specifically to A4 (artificial intelligence).
Is algorithmic efficiency relevant to machine learning, where large data sets are processed and computational cost can be significant? (Very directly: a machine learning model is typically trained on thousands or millions of records, so an algorithm inside that training process running in O(n²) instead of O(n log n) isn't a small difference, it's the difference between a model that finishes training overnight and one that doesn't finish at all. The same scalability reasoning this page applies to a photocopier room's queue applies just as literally to a data centre's training run, just at a scale where the gap between growth rates costs real time and real money.)
Revision questions
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
[2]Explain what "time complexity" and "space complexity" each measure about an algorithm.
Marking points
评分要点
- Time complexity measures how the number of operations an algorithm performs grows as the input size n grows.
- Space complexity measures how the amount of extra memory the algorithm needs grows as n grows.
- 时间复杂度衡量算法执行的操作数量如何随输入规模 n 增长。
- 空间复杂度衡量算法所需的额外内存量如何随 n 增长。
[3]An algorithm counts out to 2n² + 15n + 6 operations. State its Big O time complexity, and explain why the other two terms can be ignored.
Marking points
评分要点
- Big O time complexity is O(n squared).
- For large n the n squared term grows far faster than 15n or the constant 6, so it dominates the total.
- Big O describes the growth rate, so the constant multiplier 2 and the lower-order terms are dropped.
- 大 O 时间复杂度是 O(n 平方)。
- 对大的 n,n 平方项的增长远快于 15n 或常数 6,所以它在总量中占主导。
- 大 O 描述增长率,所以常数倍数 2 和低阶项都被舍去。
[4]A school is deciding between two algorithms: one is O(n²) but simple to implement, the other is O(n log n) but more complex. Explain which one should be chosen if the school has 40 students, and which if it later grows to 4000, justifying your answer in terms of scalability.
Marking points
评分要点
- For only 40 students n is small, so the difference in growth rate barely matters and both algorithms run almost instantly.
- The simpler O(n squared) algorithm should be chosen because it is easier to implement correctly and maintain, and the performance cost is negligible at this size.
- If the data set were large (say hundreds of thousands), the O(n log n) algorithm would be worth the extra complexity.
- 对于只有 40 名学生,n 很小,所以增长率的差异几乎无关紧要,两种算法几乎瞬间完成。
- 应选择较简单的 O(n 平方) 算法,因为它更容易正确实现和维护,而在这个规模下性能代价可以忽略。
- 如果数据集很大(比如几十万),O(n log n) 算法才值得付出额外的复杂度。
Flashcards
Active recall for this statement’s key terms.
Keep this statement’s practice with you
Free account: unlock every revision question and flashcard for B2.4.1, and every other IB CS 2027 statement.