Skip to content

counting basic steps

counting the cost

what it is

A method's cost is counted in steps, not seconds, since a count of steps belongs to the method alone.

Take a list of 4 short names, and a name to look for that is not in it. The method 'look at every name' looks at place 1, then 2, 3 and 4. At place 5 there is no name, so it says the name is not there and stops. The table writes it as four instructions, A to D.

instructionwhat it doesruns for 4 namesruns for nn names
Astart at place 111
Bif there is no name at this place, say the name wanted is not there, and stop5n+1n + 1
Clook at the name here: if it is the one wanted, say where it is, and stop4nn
Dmove on to the next place, and go back to instruction B4nn
in all143n+23n + 2
'Look at every name' on a list without the name wanted. It walks past every name to place n+1n + 1, where B finds no name and stops it.

A method like this is an algorithm: clear steps that turn what it is given, its input, into what is wanted. Here the input is a list of nn names, at places 1 to nn, and the name wanted. Every count of the method on this page is for a list without the name wanted, where the method does the most it can.

A stopwatch would time the computer, the programming language and whatever else the computer is doing, as well as the method. Time it twice and you get two answers.

A basic step is one read from memory, one write, one small working out such as comparing two short names, or one choice of what comes next. Instruction C reads the place, the name there and the name wanted, compares the two names and chooses: 5 basic steps. Every instruction does a fixed few, from 1 to 5, the same however long the list.

Instructions run are easier to count than basic steps. For 4 names, 14 instructions run against 53 basic steps. The numbers differ, but question 4 shows the basic steps stay between the instructions run and 4 times them. So both grow the same way, and question 1 counts instructions: 3n+23n + 2.

Memory is counted in cells, each holding one number or one short name. 'Look at every name' uses one cell besides its input, for the place it has reached, however long the list.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1How many instructions run when the name is not in a list of nn names?
  1. the move: follow the walk

    the line:
    the method visitsplaces 1 to n+1\begin{gathered} \text{the method visits} \\ \text{places 1 to } n + 1 \end{gathered}

    why it is allowed: A starts at place 1. At each place with a name, B lets the method through, C looks and does not stop it, and D moves it on. At place n+1n + 1, B finds no name and stops it.

  2. the move: count instruction A\mathrm{A}

    the line:
    A runs once\text{A runs once}

    why it is allowed: A runs at the start, and nothing goes back to it.

  3. the move: count instruction B\mathrm{B}

    the line:
    B runs n+1 times\text{B runs } n + 1 \text{ times}
    where the method isBCD
    place 1runsrunsruns
    place 2runsrunsruns
    place 3runsrunsruns
    place 4runsrunsruns
    place 5runs, and stops
    in all544
    A list of 4 names without the name wanted: B runs 5 times, C and D 4 times each.

    why it is allowed: B runs at every place visited, places 1 to n+1n + 1 by line 1.

  4. the move: count instruction C\mathrm{C}

    the line:
    C runs n times\text{C runs } n \text{ times}

    why it is allowed: C runs at each place with a name, places 1 to nn by line 1.

  5. the move: count instruction D\mathrm{D}

    the line:
    D runs n times\text{D runs } n \text{ times}

    why it is allowed: D runs after each run of C, since by line 1 C never stops the method.

  6. the move: add them up

    the line:
    T=1+(n+1)+n+nT = 1 + (n + 1) + n + n

    why it is allowed: Let TT count every run of every instruction. Add lines 2 to 5.

  7. the move: add like with like

    the line:
    T=3n+2T = 3n + 2

    why it is allowed: The three lots of nn make 3n3n, and the two 1s make 2. For 4 names, 14.

  8. the move: a fact for the ceiling

    the line:
    2≤2n2 \le 2n

    why it is allowed: Another way of counting gives another exact formula. So trap TT between two multiples of nn. First a fact: nn is at least 1, so 2 is at most 2n2n.

  9. the move: a ceiling

    the line:
    T≤3n+2nT \le 3n + 2n

    why it is allowed: By line 8, swap the 2 in line 7 for 2n2n, which is no smaller.

  10. the move: add like with like

    the line:
    T≤5nT \le 5n

    why it is allowed: 3n3n and 2n2n make 5n5n.

  11. the move: think of TT at every length at once

    the line:
    T for everylength of list at once\begin{gathered} T \text{ for every} \\ \text{length of list at once} \end{gathered}
    namesinstructions run5 times the names
    155
    2810
    41420
    103250
    100302500
    At every length, the count stays at or under 5 times the names.

    why it is allowed: Lines 1 to 10 hold for every list of at least one name. So read TT as the whole graph of counts against lengths, not one point.

  12. the move: read it as a bound

    the line:
    T∈O(n)T \in O(n)
    namestwice the names and 20 more3 times the names
    1223
    104030
    206060
    308090
    100220300
    A made-up count breaks the ceiling of 3 times the names below 20 names and meets it at 20. Each name more adds 2 to the count and 3 to the ceiling.

    why it is allowed: O(n)O(n) is the group of counts that, from some length on, never climb above a fixed number above zero times nn. Short lists may break it, as in the table beside this line. By line 10, 5 serves for TT. Read TT is in big O of nn.

  13. the move: a floor

    the line:
    T≥3nT \ge 3n
    102104106108101102103104105106107101102103104105106107108101102103104105106107
    • the count of instructions run, 3n+23n + 2
    • the ceiling of line 10, 5n5n
    • the floor of this line, 3n3n
    A log-log plot: each step across or up is ten times the one before. Multiplying by the same number moves up the same distance. So a line always the same multiple of another stays the same distance above it: 50 sits as far above 30 as 500 above 300. The count, the heavy line, stays between 5n5n and 3n3n.

    why it is allowed: Leave the 2 out of line 7. What is left is smaller.

  14. the move: read it as a bound

    the line:
    T∈Ω(n)T \in \Omega(n)

    why it is allowed: Ω(n)\Omega(n) is the same with a floor: from some length on, never below a fixed number above zero times nn. By line 13, 3 serves for TT. Read TT is in big omega of nn.

  15. the move: a ceiling and a floor together

    the line:
    T∈Θ(n)T \in \Theta(n)

    why it is allowed: By lines 10 and 13, TT stays between 3n3n and 5n5n; lines 12 and 14 read both as bounds with the same term, nn. That is the tight bound, read TT is in theta of nn: TT grows like nn. Counting the basic steps gives 3 and 20 instead (question 4); the term stays nn.

try it with a small nn: at n=4n = 4, line 6 gives T=14T = 14, which you can work out by hand

question 2Counted by its looks alone, does the cost grow any differently?
  1. the move: count the looks

    the line:
    there are n looks\text{there are } n \text{ looks}

    why it is allowed: Each run of C is one look at a name. By line 4 of question 1, C runs nn times.

  2. the move: instructions run for each look

    the line:
    R=3n+2nR = \frac{3n + 2}{n}
    namesinstructions runlooksinstructions for each look
    1515
    2824
    41443.5
    1,0003,0021,0003.002
    Each look costs 5 instructions at 1 name, and nearer 3 as the list grows.

    why it is allowed: Let RR be the instructions run for each look. By line 7 of question 1, 3n+23n + 2 of them are shared among nn looks.

  3. the move: divide each part

    the line:
    R=3+2nR = 3 + \frac{2}{n}

    why it is allowed: 3n3n shared among nn is 3, and 2 shared among nn is 2n\frac{2}{n}.

  4. the move: a floor

    the line:
    R≥3R \ge 3

    why it is allowed: 2n\frac{2}{n} is above zero, so leaving it out of line 3 leaves less.

  5. the move: a fact for the ceiling

    the line:
    2n≤2\frac{2}{n} \le 2

    why it is allowed: nn is at least 1, so 2 shared among nn is at most 2.

  6. the move: a ceiling

    the line:
    R≤3+2R \le 3 + 2

    why it is allowed: By line 5, swap the 2n\frac{2}{n} in line 3 for 2, which is no smaller.

  7. the move: add them

    the line:
    R≤5R \le 5
    102104106108101102103104105106107101102103104105106107108101102103104105106107
    • the count of instructions run, 3n+23n + 2
    • the looks, nn
    The instructions run and the looks on a log-log plot. The first stays 3 to 5 times the second, so the two lines run side by side.

    why it is allowed: 3 and 2 make 5.

  8. the move: read the two together

    the line:
    3 to 5 instructionsfor every look\begin{gathered} \text{3 to 5 instructions} \\ \text{for every look} \end{gathered}

    why it is allowed: By lines 4 and 7, each look costs 3 to 5 instructions. So no: counted by its looks, the cost still grows like nn.

try it with a small nn: at n=4n = 4, line 2 gives R=3.5R = 3.5, which you can work out by hand

question 3If the list is twice as long, do the instructions run double?
  1. the move: put in twice as many names

    the line:
    W=3×(2n)+2W = 3 \times (2n) + 2

    why it is allowed: Let WW be the instructions run for 2n2n names, twice as many. Put 2n2n for nn in line 7 of question 1.

  2. the move: multiply out

    the line:
    W=6n+2W = 6n + 2

    why it is allowed: 3 lots of 2n2n make 6n6n.

  3. the move: twice the instructions for n names

    the line:
    2×(3n+2)=6n+42 \times (3n + 2) = 6n + 4

    why it is allowed: Twice 3n3n is 6n6n, and twice 2 is 4.

  4. the move: write 2 as 4 take away 2

    the line:
    W=6n+4−2W = 6n + 4 - 2

    why it is allowed: Line 2, its 2 written as 4 take away 2.

  5. the move: use line 3

    the line:
    W=2×(3n+2)−2W = 2 \times (3n + 2) - 2
    namesinstructions runtwice thoseinstructions run for twice the names
    4142826
    10326462
    1,0003,0026,0046,002
    Twice the names always run 2 fewer than twice the instructions.

    why it is allowed: By line 3, 6n+46n + 4 is twice 3n+23n + 2.

  6. the move: read it for a long list

    the line:
    for a long list theydouble, near enough\begin{gathered} \text{for a long list they} \\ \text{double, near enough} \end{gathered}

    why it is allowed: The 2 matters less as the list grows: 2,000 names run 6,002 instructions, against twice 3,002, 6,004. The looks double exactly, one for each name.

try it with a small nn: at n=4n = 4, line 1 gives W=26W = 26, which you can work out by hand

question 4Counted in basic steps, one reasonable way, how many are there?
  1. the move: count each instruction's basic steps

    the line:
    A does 1 basic step,B 4, C 5 and D 3\begin{gathered} \text{A does 1 basic step,} \\ \text{B 4, C 5 and D 3} \end{gathered}
    instructionits basic stepshow many
    Awrite 1 into the cell for the place reached1
    Bread the place and the list's length, compare them (past the length there is no name), choose4
    Cread the place, the name there and the name wanted, compare the two names, choose5
    Dread the place, add one, write it back3
    Each instruction's basic steps, the same however long the list. Saying the answer is not counted.

    why it is allowed: One step for each read, write, working out and choice, as in the table. Going on to the only next instruction is no choice, so it is free. Others might count one more or less; line 16 says why that hardly matters.

  2. the move: each instruction's steps times its runs

    the line:
    1×1,4×(n+1),5×n and 3×n\begin{gathered} 1 \times 1, \quad 4 \times (n + 1), \\ 5 \times n \text{ and } 3 \times n \end{gathered}

    why it is allowed: Steps from line 1, runs from lines 2 to 5 of question 1.

  3. the move: add them up

    the line:
    S=1+4(n+1)+5n+3nS = 1 + 4(n + 1) + 5n + 3n

    why it is allowed: Let SS be the basic steps. Add the four products of line 2.

  4. the move: multiply out the bracket

    the line:
    S=1+4n+4+5n+3nS = 1 + 4n + 4 + 5n + 3n

    why it is allowed: Four times n+1n + 1 is 4n4n and 4.

  5. the move: add like with like

    the line:
    S=12n+5S = 12n + 5

    why it is allowed: 4n4n, 5n5n and 3n3n make 12n12n, and 1 and 4 make 5. For 4 names, 53.

  6. the move: a fact for the floor

    the line:
    3n≤12n3n \le 12n

    why it is allowed: The aim: the basic steps sit between the instructions run and 4 times them. For the floor, a fact: 12n12n is 3n3n and 9n9n more, and 9n9n is above zero.

  7. the move: a floor

    the line:
    S≥3n+5S \ge 3n + 5

    why it is allowed: By line 6, swap the 12n12n in line 5 for 3n3n, which is no bigger.

  8. the move: a lower floor

    the line:
    S≥3n+2S \ge 3n + 2

    why it is allowed: Swap the 5 in line 7 for 2, which is smaller. 3n+23n + 2 is the instructions run, by line 7 of question 1, so there are never fewer basic steps.

  9. the move: a ceiling

    the line:
    S≤12n+8S \le 12n + 8

    why it is allowed: 8 is more than 5. Swap the 5 in line 5 for 8.

  10. the move: four times the instructions run

    the line:
    4×(3n+2)=12n+84 \times (3n + 2) = 12n + 8

    why it is allowed: Four times 3n3n is 12n12n, and four times 2 is 8.

  11. the move: use line 10

    the line:
    S≤4×(3n+2)S \le 4 \times (3n + 2)
    namesinstructions runbasic steps4 times the instructions run
    151720
    4145356
    1032125128
    1003021,2051,208
    The basic steps always sit between the instructions run and 4 times them.

    why it is allowed: By line 10, line 9 says at most four times the instructions run.

  12. the move: the ceiling of question 1

    the line:
    3n+2≤5n3n + 2 \le 5n

    why it is allowed: By lines 7 and 10 of question 1, the instructions run are never more than 5n5n.

  13. the move: a ceiling in n alone

    the line:
    S≤4×5nS \le 4 \times 5n

    why it is allowed: By line 12, swap the 3n+23n + 2 in line 11 for 5n5n, which is no smaller.

  14. the move: multiply

    the line:
    S≤20nS \le 20n

    why it is allowed: 4 times 5n5n is 20n20n.

  15. the move: a floor in n alone

    the line:
    S≥3nS \ge 3n
    102104106108101102103104105106107101102103104105106107108109101102103104105106107
    • the basic steps, 12n+512n + 5
    • the ceiling of line 14, 20n20n
    • the floor of this line, 3n3n
    The basic steps stay between 20n20n and 3n3n, on a log-log plot as in question 1.

    why it is allowed: Leave the 2 out of line 8. What is left is smaller.

  16. the move: read them together

    the line:
    between 1 and 4 times as manybasic steps as instructions run\begin{gathered} \text{between 1 and 4 times as many} \\ \text{basic steps as instructions run} \end{gathered}

    why it is allowed: By lines 8 and 11, the basic steps sit between the instructions run and 4 times them. Count B as 5 steps instead: for 4 names B runs 5 times, so 5 steps more, 58, still between 14 and 5 times 14, 70. Every instruction does at least 1 basic step and at most a fixed few, here 5. So any such count stays between TT, question 1's instructions run, and a fixed multiple of it. Here lines 14 and 15 put SS between 3n3n and 20n20n: it grows like nn.

try it with a small nn: at n=4n = 4, line 3 gives S=53S = 53, which you can work out by hand

a worked example

Count the instructions run, and the looks, for a name that is not in a list of 4 names.

  1. A starts at place 1: 1 instruction.
  2. At each of places 1 to 4, B, C and D run once: 3 instructions a place, 12 in all, and 4 looks.
  3. At place 5 there is no name. B runs once more and stops: 1 instruction.
  4. 1, 12 and 1 make 14. 3n+23n + 2 gives 14 for 4 names too: 3 times 4 is 12, and 2 more is 14.

the trap

check yourself

  1. Two people time the same method and get different numbers. Two others count its instructions run and get the same. Why?

    the answer

    A time also measures the computer, the language and the moment. A count belongs to the method alone.

  2. One person counts 3n+23n + 2 instructions run, another only the nn looks. Do they disagree about how the cost grows?

    the answer

    No. Each look costs 3 to 5 instructions, so the two counts grow together, like nn.

  3. How many memory cells does 'look at every name' use besides its input, for 1,000 names, and for a million?

    the answer

    One each time, for the place it has reached. Its memory stays the same however long the list is.