How to use Big-O notation
Big-O describes how runtime grows with input size. Learn to recognise O(1), O(log n), O(n), and O(n²) in common algorithms.
Big-O notation describes how the running time (or the number of steps) of an algorithm grows as the input size grows. To find it, count how many times the main work repeats in terms of , then keep only the fastest-growing term and drop constants.
The common classes
From slowest growth to fastest:
- , constant. The work does not depend on . Example: reading one item of a list by its index.
- , logarithmic. The problem is halved each step. Example: binary search on a sorted list.
- , linear. One pass over the data. Example: linear search, or summing a list.
- . Efficient sorting, such as merge sort.
- , quadratic. Two nested loops over the data. Example: comparing every pair of items.
- , exponential. The work doubles with each extra item. This is only usable for very small .
Big-O is often quoted for the worst case unless the question says otherwise.
The method step by step
- Decide what is. It is the size of the input, such as the length of a list.
- Find the repeated work. Look for loops and recursion. Statements outside any loop run a fixed number of times.
- Count the repetitions in terms of . One loop over items repeats times. A loop inside another multiplies the counts.
- Add the parts. If one part runs after another, add their counts.
- Drop constants and smaller terms. becomes . becomes .
- Name the class.
Dropping constants is not saying they are unimportant. Big-O describes the shape of the growth for large , and constants do not change that shape.
Worked example 1: a single loop
total = 0
count = 0
for x in items:
total += x
count += 1
print(x)
Suppose items has elements. The loop body has 3 statements, so the lines inside the loop run times.
- Body repeats times.
- Total statements in the loop: .
- In general this is steps.
- Drop the constant 3: the algorithm is .
If doubles to 100, the work doubles to 300. This is linear growth.
Worked example 2: nested loops
for i in range(n):
for j in range(i):
compare(i, j)
The inner loop runs i times for each value of i. With :
- For the inner loop runs 0 times, for it runs 1 time, and so on up to , which runs 9 times.
- Total: comparisons.
- In general the total is .
- Expand: . Drop the smaller term and the constant .
The algorithm is . It is half of , but constants do not change the class.
Worked example 3: binary search
Binary search looks at the middle of a sorted list. If the target is smaller it keeps the left half, and if larger it keeps the right half. How many items does it need to check in the worst case for a list of 1 024 items, compared with linear search?
- Each check halves the number of items left: 1 024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1.
- Because , ten checks can shrink the list to a single item, and one more check settles that item.
- Worst case for binary search: checks.
- Worst case for linear search: 1 024 checks.
Binary search is . If the list doubles to 2 048 items, it needs only one more check, 12. Linear search needs 2 048.
This only works if the list is sorted.
Worked example 4: predicting how long it will take
An algorithm is . It takes 4 seconds when . Estimate the time when .
- The input is times larger.
- For the time grows by the square of that factor: .
- Estimated time: seconds.
Compare an algorithm that also takes 4 seconds at . It would take seconds at .
This is an estimate. Big-O gives the shape of growth, not exact times, and it ignores constants and small inputs.
Common mistakes
- Saying or . Drop constants and write .
- Adding when you should multiply. Nested loops multiply, and sequential loops add.
- Claiming that two loops always mean . Two loops one after the other are . Only nested loops give .
- Forgetting that the inner loop may depend on the outer one, as in example 2.
- Forgetting that binary search needs a sorted list.
- Thinking that Big-O gives the exact time in seconds. It describes how the time grows.
- Mixing up and . If the problem is halved each step, it is logarithmic.
Exam tips
- Say what is, then count the repeats in terms of .
- Use the vocabulary the question asks for: constant, logarithmic, linear, quadratic.
- To compare two algorithms, name both classes and say which grows more slowly.
- A loop that halves its counter, such as
while n > 1: n = n // 2, is logarithmic. - Show the working for any numeric estimate, including the scale factor.
Frequently asked questions
Does Big-O always describe the worst case?
By convention it is often used for the worst case, but it can also describe the best or average case. If the question says "worst case" or "average case", follow it.
Why do we ignore constants?
For large inputs, the growth rate matters much more than a fixed factor. An algorithm eventually beats an one, however large the constant.
What is the Big-O of a loop inside a loop?
If both loops run about times, the total is , so . If the inner loop runs a fixed number of times, such as 5, it is .
Is Big-O only about time?
No. It can also describe the memory an algorithm uses, which is called space complexity.
What is the difference between O(n) and O(log n)?
In an algorithm, doubling the input doubles the work. In an algorithm, doubling the input adds only one more step.
To practise, try the Big-O notation questions here and state the repeated work before you pick the class.
Put it into practice
Practise this topic with questions and get an explanation after each answer.
Practise Big-O notation & complexity analysis →