Unit 4: Algorithms · Evaluating algorithms

Article

Counting the steps

7 min read

Two programs both find a name in your phone's contact list. One takes a blink. The other makes you wait. A program that gives the right answer is good, but a program that gives it quickly is better. How do we measure "quickly" in a fair way?

Count steps, not seconds

You could time a program with a stopwatch, but the answer changes with the computer, the language and what else is running. A phone and a supercomputer would give different times for the same algorithm.

So we count steps instead. The efficiency of an algorithm is how many steps it needs, and how that number grows as the input gets bigger. We call the size of the input n. For a list, n is the number of items.

Counting every tiny action is slow work, so we count the one that repeats most: how many times does the loop body run? Here is a loop that adds up a list:

AP pseudocode
total ← 0
FOR EACH item IN aList
{
    total ← total + item
}

For a list of n items, the body runs n times. Let the computer count for you:

Runs in your browser

Double the list and the work doubles. This is the most common pattern.

Check your understanding

How many times does the statement total ← total + item run when this code runs?

AP pseudocode
aList ← [4, 7, 1, 9, 3, 6, 2, 8]
total ← 0
FOR EACH item IN aList
{
    total ← total + item
}

Counting different loops

Here are the loops you will meet. The last column shows n = 10.

CodeBody runsFor n = 10
REPEAT 5 TIMES5, however big n is5
FOR EACH item IN aListn10
one loop over the list, then another loop over the listn + n = 2n20
REPEAT 3 TIMES around a loop over the list3 × n30
a loop over the list inside a loop over the listn × n100
an index loop that goes up by 2 (i ← i + 2)about n / 25

Two things to notice. A loop with a fixed count (REPEAT 5 TIMES) does the same work for any n. And when a loop is inside another loop, you multiply: the inner loop runs completely for each pass of the outer loop. Run this and watch the counts grow:

Runs in your browser

You get 9, 25 and 100. Ten times more items would give a hundred times more work.

We care about the pattern, not the exact count. 2n, 3n and n + 5 all grow in step with n: double n, and the steps roughly double. But n × n quadruples when n doubles.

Best case and worst case

Sometimes the number of steps depends on which input you get, not only on its size. A linear search checks the items one by one until it finds the target. For [4, 9, 2, 7, 5]:

TargetItems checked
41 (the best case: it is first)
74
55
6 (not in the list)5 (the worst case: all of them)

The best case is the luckiest input. The worst case is the unluckiest: here, the target is last, or not there at all. For a list of n items, the best case is 1 check and the worst case is n checks. We usually describe an algorithm by its worst case, because that is a promise: it never takes more than that.

Check your understanding

A linear search looks for a target in a list of 100 items. The target is not in the list. How many items does the search check?

Worked example: which algorithm is faster?

Two algorithms find the largest item. Algorithm A walks through the list once, keeping the biggest item seen so far (one loop). Algorithm B takes each item and compares it with every item to see if it beats them all (a loop inside a loop). Compare their steps for a list of 1,000 items.

Stepsn = 1,000
An1,000
Bn × n1,000,000

B does 1,000 times as much work. With 10 items the difference is small (10 against 100), but it grows fast. This is why we look at how steps grow with n.

Common mistakes

  • Measuring with a clock. Time depends on the computer. Steps do not.
  • Adding when you should multiply. A loop inside a loop is n × n, not n + n.
  • Counting the stop check as a pass. REPEAT UNTIL checks one more time at the end, but the body does not run then.
  • Off by one. From 1 to n there are n passes, not n − 1.
  • Mixing up best and worst case. The worst case is the most steps, not the fewest.

On the exam

Typical questions: "How many times does this loop body run for a list of n items?", "Which algorithm takes fewer steps?", and "What is the maximum number of items checked?" Write the count for a small input first (for example n = 4), then check your answer against it. When you compare algorithms, look at large n.

Key words

Efficiency: the number of steps an algorithm needs, and how it grows with the size of the input.

n: the size of the input (for a list, the number of items).

Best case: the input for which the algorithm needs the fewest steps.

Worst case: the input for which it needs the most steps.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.