Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 17: Algorithm Analysis — Comparing Searching Algorithms

Two algorithms can both be correct and yet be very different in how much work they do, and choosing between them is a skill in its own right. The work of a searching or sorting algorithm is usually measured by counting comparisons, or more generally by how the number of steps grows as the input grows. A linear search checks elements one by one, so its work grows in direct proportion to the size of the data: twice the data means twice the work in the worst case. A binary search halves the data at each step, so its work grows much more slowly: twice the data adds only about one more comparison.

The distinction that matters most is between the worst case, the average case, and the best case. The best case of a linear search is finding the target at the first position, a single comparison, but that says little about the algorithm; the worst case is checking every element, and the average case, for a target equally likely to be anywhere, is checking about half of them. For binary search, the best case is also one comparison, the worst case is about log₂(n) comparisons, and the average case is close to the worst. When you compare algorithms, compare their worst cases unless the question says otherwise, because the worst case is the guarantee you can rely on.

It helps to think in terms of growth rather than exact counts. If an algorithm's work doubles when the input doubles, it grows linearly; if it grows by a roughly constant amount when the input doubles, it grows logarithmically, which is far better for large inputs. Computer scientists describe this with Big-O notation, but the intuition is what matters here: a logarithmic algorithm stays fast even as data grows enormously, while a linear one slows in step with the data, and a quadratic one — where doubling the input quadruples the work — slows much faster still. The sorting algorithms in the next lesson show all three kinds of growth.

The conditions for using an algorithm are as important as its speed. Binary search is dramatically faster than linear search, but it requires sorted data, and sorting the data first costs work of its own — so for a single search of unsorted data, a linear search is the right choice, while for many searches of the same data, sorting once and then using binary search wins. Similarly, a simple algorithm that is easy to get right may be better than a fast one that is easy to get wrong, especially when the data are small. In free-response answers, name the algorithm, state its worst-case behaviour, and say what it requires of the data.

A worked example

Worked example: consider a phone book with one million names, sorted alphabetically. A linear search for a name could need up to one million comparisons, and on average about five hundred thousand. A binary search needs at most about twenty comparisons, because 2^20 is about one million, so twenty halvings reduce the range to a single name. That is the power of halving: each comparison discards half the remaining data, so the work grows by only one comparison each time the data double. If the phone book were unsorted, binary search could not be used at all, and the linear search would be the only option.

When you are asked to compare two algorithms, structure the answer around three points: what each one does, how much work each does in the worst case, and what each requires. A comparison that only says one is faster is incomplete; a comparison that also says when the faster one cannot be used shows real understanding. And remember that the input size matters: for ten elements, the difference between ten comparisons and four is trivial, and simplicity wins; for ten million, it is the difference between an instant answer and a noticeable wait.

Key Terms

Worst case
The most work an algorithm can do on an input of a given size; the usual basis for comparison.
Average case
The typical work an algorithm does, averaged over all possible inputs.
Best case
The least work an algorithm can do, usually not a useful basis for comparison.
Linear growth
Work growing in proportion to the input size, as in linear search.
Logarithmic growth
Work growing by a constant amount when the input doubles, as in binary search.

Discussion Questions

  1. Why is the worst case the usual basis for comparing algorithms?
  2. Binary search is faster than linear search. When would you still use linear search?
  3. What does it mean to say an algorithm's work grows logarithmically?

Practice Questions

  1. A linear search of 1,000 elements has worst-case cost 1,000 comparisons. What is the worst case for 2,000 elements?
  2. About how many comparisons does binary search need at most for 1,024 elements?
  3. What is the best case for a linear search?
  4. Why can't binary search be used on unsorted data?
  5. An algorithm takes 4 steps for 10 elements and 8 steps for 20 elements. What kind of growth is that?
Show answers
  1. 2,000 comparisons, since the work grows in proportion to the input size.
  2. About 10, since 2^10 = 1,024.
  3. One comparison, when the target is the first element checked.
  4. Because halving relies on the order: it assumes everything left of the middle is smaller and everything right is larger.
  5. Linear growth, since doubling the input doubles the work.
Keep reading to complete this lesson