How many steps does a search take?
An algorithm is a list of steps that solves a problem. Time complexity says how the number of steps grows as the input gets bigger. It matters because a method that feels instant on ten items can freeze on ten million.
A shop's search page looks through its products every time someone types. Counting the steps tells you, before you run anything, whether it will still work when the shop grows.
By the end of this lesson
Note
I can count the steps of an algorithm and say how the count grows.
Key words
| Word | What it means | Example |
|---|---|---|
| Algorithm | a list of steps that solves a problem | |
| Step | one small action, such as one comparison | |
| Input size | how much data the algorithm is given, called n | n = 1000 |
| Time complexity | how the number of steps grows as n grows | |
| Big O notation | a short way to write the growth | O(n) |
| Growth | how much the steps rise when n rises |
The cells students fill in are left blank in this preview.
We count steps, not seconds
A step is one small action: one comparison, one addition, one list item looked at. Seconds change with the machine, but the number of steps does not.
So to judge an algorithm you count its steps for an input of size n. For a list, n is the number of items.
Count the steps of a loop
# Count the steps of a loop that adds up some prices.
prices = [4, 9, 2, 7, 5]
steps = 0
total = 0
for price in prices:
total = total + price
steps = steps + 1
print("total", total, "steps", steps)
# Change prices to ten numbers, then press Run again.
Growth shows how a method scales
What matters is how the count changes when n doubles. A loop over the list takes twice as many steps for twice the items. That is linear growth.
A loop inside a loop takes n times n steps. Double the items and the steps become four times as many. That is quadratic growth.
Count a loop inside a loop
# Count the steps of a loop inside a loop.
n = 5
steps = 0
for i in range(n):
for j in range(n):
steps = steps + 1
print("n", n, "steps", steps)
# Change n to 10, then press Run again.
Worked example: steps as n grows
| Growth | n = 10 | n = 100 | n = 1000 |
|---|---|---|---|
| Constant: one step | 1 | 1 | 1 |
| Logarithmic: halve until one is left | 3 | 6 | 9 |
| Linear: one pass | 10 | 100 | 1,000 |
| Quadratic: a loop inside a loop | 100 | 10,000 | 1,000,000 |
The cells students fill in are left blank in this preview.
Let the terminal count for you
In the lab, seq 1 5 writes the numbers 1 to 5, one per line, and wc -l counts lines. Together they count to five.
The second Run does the quadratic sum: a thousand rows of a thousand columns is a million cells.
Count five lines
seq 1 5 | wc -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A thousand times a thousand
python3 -c "print(1000 * 1000)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Measure the time on your own machine
Steps are exact. Time is not, because it depends on the machine and on what else it is doing.
The time word before a command prints three lines after it ends: real, user and sys. Read the real line. The numbers will be yours alone, so run it twice and expect small differences.
Time one hundred thousand steps
time seq 1 100000 | wc -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Time ten times as many steps
time seq 1 1000000 | wc -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Big O keeps the fastest growing part
Big O notation is a short way to write the growth. You drop the small parts and the constant numbers, because for a big n they hardly matter.
An algorithm of 3n + 5 steps is O(n), said "order n". One of n times n plus 3n steps is O(n squared).
A common mistake
Warning
Many people think the method that ran faster on their laptop is the better algorithm. Seconds depend on the machine and on the one size you tried. Compare the number of steps as n grows, because that stays true on any machine.