Module 18 · Algorithms and Problem Solving — topic 5 of 18
Time complexity
Measuring how an algorithm's running time grows with input size: counting basic operations, best, average and worst cases, constant, linear, quadratic and logarithmic growth, and why growth matters more than raw speed.
Learning objectives
- Count operations to estimate running time
- Distinguish best, average and worst cases
- Recognise constant, logarithmic, linear and quadratic algorithms
Definition
Time complexity describes how the number of basic operations an algorithm performs grows as the input size n increases. It focuses on growth rate rather than exact seconds, which depend on the computer.
Introduction
An algorithm that takes one second for 1,000 items might take one second or eleven days for a million items, depending on how its work grows. Time complexity predicts which.
Why it matters
Choosing an algorithm with better growth is usually the biggest speed-up available — far more than faster hardware.
Detailed explanation
Measure input size as n: the length of a list, the number of characters, the number of users.
Count basic operations such as comparisons and assignments as a function of n instead of timing in seconds.
A single loop over n items does about n operations (linear). A loop inside a loop over the same items does about n × n (quadratic).
Halving the remaining work each step, as binary search does, takes about log₂ n steps (logarithmic) — only 20 steps for a million items.
Operations that don't depend on n, such as indexing a list or a dictionary lookup, are constant time.
The worst case is the maximum work for any input of size n; the best case the minimum; the average case the expected work. The worst case is usually quoted because it's a guarantee.
Constant factors matter less than growth: for large n, a quadratic algorithm loses to a linear one no matter how optimised it is.
Big-O notation (next lessons) is the standard way to write these growth rates.
Syntax
- Count operations, not seconds.
- Nested loops multiply.
- Halving gives log n.
Code examples
Linear versus quadratic
10 10 100 100 100 10000 1000 1000 1000000
Same answer, different growth
True True
More examples
Linear search versus binary search steps
1000 1000 10 1000000 1000000 20
Step-by-step explanation
- Searching for the last item is the worst case for linear search: every item is checked.
- Binary search halves the range each step, so a thousandfold increase in n adds only about ten steps.
- The values searched are 0 to n − 1, so each value equals its index and no list is needed.
Expected output
Running “Linear search versus binary search steps” prints exactly:
Practical applications
- Database indexes turn linear scans into logarithmic lookups.
- Replacing nested loops with dictionaries to speed up reports.
Best practices
- Count how work grows with n.
- Watch for hidden loops in built-in operations.
- Optimise the algorithm before micro-optimising code.
Common mistakes
Hidden loops
Avoid
Prefer
in on a list is linear, making the whole thing quadratic; a set lookup is constant on average.
Important notes
- Timing with time.perf_counter is still useful to confirm predictions on real data.
Key takeaways
- Time complexity measures growth with n.
- Loops in loops multiply; halving gives log n.
- Worst-case analysis gives guarantees.
Try it yourself
- 1Count the operations of a function that prints every pair of items in a list.
- 2Rewrite a quadratic 'common elements' function to be linear using a set.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedA loop over a list checks item in other_list for each item. With both lists of length n, the complexity is…