What is Big O notation?
Big O notation describes how an algorithm's running time grows as the input size n increases, usually in the worst case. To find it, keep the fastest-growing term and drop constants and lower-order terms. This is AQA A Level Computer Science (7517), section 4.4.4.3, which is A-level content. Example: An algorithm takes 3n² + 5n + 2 steps. Fastest-growing term: 3n² Drop the constant 3. Answer: O(n²)
Quick answer: Big O notation describes how the running time of an algorithm grows as the size of the input, n, grows. It is usually quoted for the worst case. To find it, keep the fastest-growing term and drop constants and lower-order terms.
What the specification expects
Big-O notation is in 4.4.4.3 Order of complexity of the AQA A-level Computer Science specification (7517), within 4.4.4 Classification of algorithms. It is part of the A-level content, not the AS (7516). You need to:
- use Big-O notation to express time complexity for constant, logarithmic, linear, polynomial and exponential time (4.4.4.3)
- be able to derive the time complexity of an algorithm (4.4.4.3)
- compare algorithms by expressing their complexity as a function of the size of the problem (4.4.4.1)
- be familiar with linear (y = 2x), polynomial (y = 2x²), exponential (y = 2ⁿ) and logarithmic (y = log₁₀ x) functions (4.4.4.2)
- know that problems with a polynomial (or less) time solution are tractable and problems with no such solution are intractable (4.4.4.5)
- know the complexities the specification gives for named algorithms: linear search O(n), binary search O(log n), bubble sort O(n²) and merge sort O(n log n) (4.3.4, 4.3.5)
Method
- Decide what n is: the size of the input, such as the length of a list.
- Count how the number of steps depends on n. Nested loops multiply their counts. Statements that follow one another add their counts.
- Write the count as a formula in n.
- Keep only the fastest-growing term and drop the lower-order terms.
- Drop any constant multiplier, then write the result as O( ).
- Name it: O(1) constant, O(log n) logarithmic, O(n) linear, O(n²) polynomial, O(2ⁿ) exponential.
Worked example 1: simplifying a formula
An algorithm takes 3n² + 5n + 2 steps. State its time complexity.
- Find the fastest-growing term: 3n².
- Drop the lower-order terms 5n and 2.
- Drop the constant multiplier 3.
Answer: O(n²)
Worked example 2: deriving the complexity of nested loops
What is the time complexity of this code, where n is the size of the input? Count the number of times the inner statement runs.
- The outer loop runs n times.
- For each pass of the outer loop, the inner loop also runs n times.
- The inner statement therefore runs n × n = n² times. For n = 5 it runs 25 times, and for n = 10 it runs 100 times.
- Doubling n multiplies the work by 4, which is polynomial growth.
total = 0
for i in range(n):
for j in range(n):
total = total + 1Answer: O(n²)
Worked example 3: deriving a logarithmic complexity
What is the time complexity of this code? Count how many times the loop runs.
- Each pass halves n, because of n = n // 2.
- For n = 16 the values are 16, 8, 4, 2, 1, so the loop runs 4 times.
- For n = 1024 the loop runs 10 times. Multiplying n by 64 only adds 6 passes.
- The number of passes is about log₂ n.
count = 0
while n > 1:
n = n // 2
count = count + 1Answer: O(log n)
Worked example 4: comparing growth
Compare O(1), O(log n), O(n), O(n log n), O(n²) and O(2ⁿ) when n = 16.
- O(1): 1 step.
- O(log n): log₂ 16 = 4 steps.
- O(n): 16 steps. O(n log n): 16 × 4 = 64 steps.
- O(n²): 16² = 256 steps.
- O(2ⁿ): 2¹⁶ = 65 536 steps.
Answer: The order from slowest-growing to fastest-growing is O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ). Exponential growth is already 65 536 for n = 16.
Common mistakes
- Keeping constants, such as writing O(3n²) instead of O(n²).
- Keeping lower-order terms, such as writing O(n² + n) instead of O(n²).
- Treating Big O as an exact number of seconds. It describes how the time grows with n.
- Adding the counts of nested loops instead of multiplying them.
- Mixing up the complexity of the named algorithms, such as linear search O(n) and binary search O(log n).
FAQs
What does n stand for?
n is the size of the problem, such as the number of items in a list. Big O shows how the running time changes as n grows.
What are tractable and intractable problems?
The specification says that problems with a polynomial (or less) time solution are tractable, and problems with no polynomial (or less) time solution are intractable. Heuristic methods are often used to tackle intractable problems.
Is Big O only about time?
The specification asks you to use Big-O notation to express time complexity. It also says that algorithms can be compared space-wise, for example by how much memory they use (4.4.4.1).
How does a doubled input change linear search and binary search?
Doubling the list doubles the work for a linear search, which is O(n). A binary search, which is O(log n), needs about one extra comparison: 1 000 items need at most 10 comparisons and 2 000 items need at most 11.
Want one-to-one help? Find an online Computer Science tutor with ExpertTutor.
Want one-to-one support?
One-to-one online tuition can help you build confidence in your subject. Find a tutor who teaches your course.
Find a TutorLearn face to face in Glasgow
Prefer in-person lessons? Visit the ExpertTutor Glasgow Southside centre.
See the centre