Skip to content

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.

Beginner 10 min read·Lesson 265 of 306 in Python Programming

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

Python
for x in items: # n steps → linear
...
for a in items: # n × n steps → quadratic
for b in items:
...
while n > 1: # log n steps → logarithmic
n //= 2
  • Count operations, not seconds.
  • Nested loops multiply.
  • Halving gives log n.

Code examples

Linear versus quadratic

Python
def count_linear(n):
steps = 0
for _ in range(n):
steps += 1
return steps
def count_nested(n):
steps = 0
for _ in range(n):
for _ in range(n):
steps += 1
return steps
for n in [10, 100, 1000]:
print(n, count_linear(n), count_nested(n))
Output
10 10 100
100 100 10000
1000 1000 1000000

Same answer, different growth

Python
def has_dup_slow(xs):
return any(xs[i] == xs[j] for i in range(len(xs)) for j in range(i + 1, len(xs)))
def has_dup_fast(xs):
return len(set(xs)) != len(xs)
data = list(range(2000)) + [5]
print(has_dup_slow(data), has_dup_fast(data))
Output
True True

More examples

Linear search versus binary search steps

Python
def linear_steps(n, target):
steps = 0
for x in range(n):
steps += 1
if x == target:
break
return steps
def binary_steps(n, target):
lo, hi, steps = 0, n - 1, 0
while lo <= hi:
steps += 1
mid = (lo + hi) // 2
if mid == target:
break
if mid < target:
lo = mid + 1
else:
hi = mid - 1
return steps
for n in [1_000, 1_000_000]:
print(n, linear_steps(n, n - 1), binary_steps(n, n - 1))
Output
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:

TextOutput
1000 1000 10
1000000 1000000 20

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

for x in items:
if x in other_list: # another loop inside!
...

Prefer

other = set(other_list)
for x in items:
if x in other:
...

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

  1. 1
    Count the operations of a function that prints every pair of items in a list.
  2. 2
    Rewrite 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…
Practise this topic