Sub topic 3
How Long Linear Search Takes
This chapter assumes you can read basic Python and know what a list index is. It also assumes you have seen Big-O notation, which describes how running time grows as the input grows. You should already know the linear_search function from the previous chapter. Here we measure how much work that function does.
Counting Comparisons
We measure linear search by counting comparisons, meaning the number of times it checks value == target. Each comparison costs about the same, and every other step in the loop runs once per comparison. So the comparison count tracks the running time closely.
The count depends on where the target is in the list, or whether it is there at all. That is why we split the analysis into three cases.
Best, Worst, and Average Case
| Case | When it happens | Comparisons | Big-O |
|---|---|---|---|
| Best | Target is at index 0 | 1 | O(1) |
| Worst | Target is last, or missing | n | O(n) |
| Average (target present) | Each position equally likely | (n + 1) / 2 | O(n) |
| Average (target missing) | Every search fails | n | O(n) |
The average case needs a short derivation. Suppose the target is in the list and is equally likely to be at any of the n positions. Finding it at index i takes i + 1 comparisons. The average is:
(1 + 2 + ... + n) / n = (n(n + 1) / 2) / n = (n + 1) / 2
On average, a successful search checks about half the list. Big-O drops the constant factor of one half, so the average case is still O(n). If you double the list size, the average work also doubles.
Measuring It Yourself
This version counts comparisons so you can check the formulas against real runs.
import random
def linear_search_count(items, target):
comparisons = 0
for i, value in enumerate(items):
comparisons += 1
if value == target:
return i, comparisons
return -1, comparisons
n = 1000
items = list(range(n))
# Best and worst cases
print(linear_search_count(items, 0)) # (0, 1)
print(linear_search_count(items, 999)) # (999, 1000)
print(linear_search_count(items, -5)) # (-1, 1000)
# Average case over many random targets that are present
trials = 10000
total = sum(linear_search_count(items, random.randrange(n))[1] for _ in range(trials))
print(total / trials) # close to (n + 1) / 2 = 500.5The last line prints a number close to 500.5. The exact value changes from run to run because the targets are random.
How the Cost Grows
Both lines are straight, which is what linear growth means. A list ten times larger costs ten times as many comparisons.
Common Mistakes
- Forgetting failed searches. If most of your lookups miss, every one of them costs n comparisons. In that situation the "half the list" figure does not apply.
- Assuming a uniform distribution. The (n + 1) / 2 result depends on every position being equally likely. If popular items sit near the front, real searches cost less. If they sit near the back, they cost more.
- Ignoring hidden loops. In Python,
target in itemsanditems.index(target)are both linear searches. If you call one inside a loop over n items, the total cost becomes O(n²). - Counting space wrong. Linear search uses O(1) extra memory, just a loop index. It does not copy the list.
Space and Preconditions
Linear search needs no sorting and no extra memory, and it works on any sequence you can step through, including linked lists and streams. These properties come back in the final chapter, where they help decide when linear search is the right choice.
Exercise
A list holds 200 items. A program runs 100 searches: 60 find their target (each position equally likely), and 40 search for values that are not in the list. About how many comparisons does linear search make in total?
Show answer
Each successful search averages (200 + 1) / 2 = 100.5 comparisons, so 60 × 100.5 = 6,030. Each failed search costs 200 comparisons, so 40 × 200 = 8,000. The total is about 14,030 comparisons.
Exercise
This code checks, for each item in list a, whether it also appears in list b. Both lists have n items. What is its running time, and why? for x in a: if x in b: print(x)
Show answer
O(n²). The expression x in b is a linear search costing up to n comparisons, and it runs once for each of the n items in a, so the total is up to n × n comparisons.