Counting steps: O(1), O(n) and O(n²)
🌍 In real life. At a wedding with n guests:
- calling out "Who is at the door?" and looking once takes the same effort however many guests there are
- looking for your cousin by walking past every guest takes longer the more guests there are: twice the guests, twice the walk
- if every guest shakes hands with every other guest, the handshakes pile up fast: twice the guests, about four times the handshakes
These three are the most common shapes of work in programs, and Big-O is the name for them.
Why count steps, not seconds? The same program runs faster on a new laptop than on an old phone, so seconds don't describe the program. Instead we count basic steps (a comparison, an addition, reading items[i]) as the input size n grows.
The idea in one sentence: Big-O describes how the number of steps grows as n grows, keeping only the part that matters for big n.
- O(1), constant: the same number of steps whatever n is
- O(n), linear: steps grow in step with n
- O(n²), quadratic: steps grow with n × n
Its words:
- n: the size of the input: how many items, letters or rows
- basic step: one small action that takes about the same time every time: a comparison, an addition, reading one item
- worst case: the input that makes the code do the most steps; Big-O usually describes this one
- growth rate: how the step count changes when n doubles: not at all, twice as many, or four times as many
Watch each one run. The gold line is the one that does the work, and its counter shows how many times it ran.
Keep only what matters for big n. Suppose a function does 3 steps for each item plus 5 steps of setup: 3n + 5 steps. For n = 1,000,000 the + 5 is nothing, and the 3 only changes the speed by a fixed factor, not the shape. So we drop both: 3n + 5 is O(n). In the same way n² + n is O(n²): for big n the n² part is all that counts.
Worst case. Searching a list for a value might find it first try, or only at the very end, or not at all. Big-O usually describes the worst case, the most steps it could take: searching n items is O(n).
def find(items, target):
checks = 0
for i, x in enumerate(items):
checks += 1
if x == target:
return i, checks
return -1, checks
guests = ["Mina", "Rafi", "Tara", "Zara", "Adi"]
print(find(guests, "Mina"))
print(find(guests, "Adi"))
print(find(guests, "Nobody"))Mistakes and edge cases. Two loops one after the other are n + n = 2n steps: still O(n), not O(n²). It is only O(n²) when one loop runs inside the other. And keep the biggest term, not the first one you see: n + n² is O(n²).
Quiz
What is the Big-O of
def last(items): return items[-1]?- O(1)
- O(n)
- O(n²)
How many times does the inner line run? What does this print?
steps = 0 for a in range(5): for b in range(5): steps += 1 print(steps)A function takes 4n + 10 steps. What is its Big-O?
- O(n)
- O(4n + 10)
- O(10)
What does this print?
steps = 0 for x in range(6): steps += 1 for y in range(6): steps += 1 print(steps)Doubling n roughly does what to an O(n²) function's steps?
- Multiplies them by about 4
- Doubles them
- Adds one more step
This should count the pairs (i, j) with i < j: 6 for four items. It prints 16. Which line is wrong?
items = ["a", "b", "c", "d"] pairs = 0 for i in range(len(items)): for j in range(len(items)): pairs += 1 print(pairs)
Coding tasks
Write
find(items, target)that walks the list and gives back a pair: the position oftarget(or-1) and how many items it checked.Write
handshakes(people)that gives back every pair of different people as a list of tuples(a, b)withabeforebin the list. (For n people there are n(n-1)/2 pairs: this is O(n²).)Name the growth: write
big_o(steps_at_10, steps_at_20)that gives back"O(1)"if the steps stayed about the same when n doubled from 10 to 20,"O(n)"if they about doubled, and"O(n²)"if they became about four times as many (real counts are rarely exact: 45 → 190 is O(n²)).