Skip to content

Counting steps

Count the basic operations a function performs as a function of n, and compare two such counts, such as 3n + 2 and n², as n grows.

It opens on

This function adds up the numbers in a list a. For a list of 10 numbers, will it take about 10 steps, about 30 steps, or about 100 steps?

Why it matters

Seconds on a laptop depend on the laptop, but the number of steps a function takes as its input grows does not, and it is what tells you before you run it whether a program will still finish when the list has a million items.

Good to know first

  • Defining and calling functions: def, parameters and return
  • for loops and range: for x in a runs its block once for each item
  • Traversing lists: a loop visits the items of a list in order and keeps a running total

Step by step

  1. Count the steps of sum_list

    Count the steps sum_list takes on a list of n numbers and write the count as a formula in n.

    Call it on a = [5, 2, 7, 1], so n, the number of items, is 4. The line s = 0 is one assignment, so after it we have used 1 step.

    the first row of the table

    A common mix-up

    The steps depend on which numbers are in the list.

    In fact: The count is 3n + 2 for any list of n numbers, because every item costs the same 3 steps whatever its value.

  2. Count a second function and compare

    Count a second function, has_dup, with a nested loop, and say which of 3n + 2 and n² is larger for a given n and for large n.

    Take a list of three different items, [4, 8, 3]. For i = 0 the inner loop passes through j = 0, 1 and 2, which is 3 passes, and the same for i = 1 and i = 2, so the function makes 3 × 3 = 9 passes and returns False.

    A common mix-up

    A function with n² steps is always slower than one with 3n + 2.

    In fact: For n = 1, 2 and 3 it is the other way round, and n² wins only from n = 4 on, which is why we look at large n.

  3. The same n, different lists

    Explain why a count can depend on the list as well as on n, and give the best case and the worst case of has_dup.

    On [7, 7, 3, 9], i = 0 and j = 0 is pass 1, which fails because i equals j, and i = 0 and j = 1 is pass 2, which finds 7 and 7, so it returns True after 2 passes. On [4, 8, 3, 4] the equal items are the first and the last, which i = 0 meets at j = 3, after 4 passes.

    the first two rows of the table

    A common mix-up

    A function has one count for each n.

    In fact: The count may depend on the contents too, so we say best case, worst case, or average, and a bare count means the worst case.

Try it

How many steps does sum_list take on a list of 6 numbers?Answer

20

Hint: 3 × 6 + 2

How many passes does has_dup make on the list [6, 1, 9, 4, 2] with no equal items, and how many steps would sum_list take on it?Answer

25 passes, and 17 steps

Hint: n = 5; n² and 3n + 2

How many passes does has_dup make on [2, 5, 8, 2, 1]?Answer

4

Hint: i = 0 meets the second 2 at j = 3, which is pass 4

Now the item 7 comes. What are s and the steps so far after it?Answer

s is 14 and the steps so far are 10, since the item costs 3 more.

At n = 4, which count is larger, 3n + 2 or n², and by how much?Answer

n² is larger, 16 against 14, by 2.

Counting steps

30 minutes every month. No card.