Sub topic 4
Why Inputs of the Same Size Can Cost Different Amounts
This chapter assumes you can read Python loops, index into lists, and add up sums like 1 + 2 + ... + n. It also assumes you already count basic operations as a function of input size n. Here we look at what that function leaves out.
One size, many inputs
A running time function T(n) looks like a single formula. But an algorithm does not receive "a size". It receives a specific input. Many different inputs share the same size n, and the algorithm may do different amounts of work on each one.
Take linear search. The input size is the list length n. Here are three inputs with n = 5 and the same target, 7:
[7, 3, 9, 1, 5]finds 7 at once, after 1 comparison.[3, 9, 1, 5, 7]finds 7 last, after 5 comparisons.[3, 9, 1, 5, 2]never finds 7, so it makes 5 comparisons.
All three lists have size 5, yet the costs are 1, 5, and 5.
Seeing it in code
The function below returns both the result and the number of comparisons made.
def linear_search(items, target):
comparisons = 0
for i, x in enumerate(items):
comparisons += 1
if x == target:
return i, comparisons
return -1, comparisons
items = [7, 3, 9, 1, 5]
print(linear_search(items, 7)) # (0, 1)
print(linear_search(items, 5)) # (4, 5)
print(linear_search(items, 42)) # (-1, 5)The list never changes. Only the target changes, and the comparison count moves between 1 and 5.
What causes the variation
The cost depends on the input's content or arrangement, not only its size. Two common sources:
- Where the answer sits. Linear search stops as soon as it finds the target. Its position decides how many steps run.
- How ordered the data already is. Insertion sort moves each element left past larger neighbors. A list that is already sorted needs no moves. A reversed list needs the most.
The loop's exit condition is usually the cause. If a loop can end early depending on the data, cost varies with the data.
Why a single T(n) is not enough
Because several inputs share the size n, "the running time for size n" is ambiguous. To give a definite answer, we must say which inputs of size n we mean. The next chapters make three choices:
| Choice | Which inputs of size n | Question it answers |
|---|---|---|
| Best case | The cheapest one | What is the least work possible? |
| Worst case | The most expensive one | What is the most work possible? |
| Average case | All of them, weighted by likelihood | What work should I expect? |
Exercise
Insertion sort receives two lists of the same length n. One is already sorted and the other is in reverse order. Will the number of comparisons be the same? Explain which property of the input causes the difference.
Show answer
No. The sorted list makes only n - 1 comparisons, because each element is compared once with its left neighbor and the inner loop stops at once. The reversed list makes n(n-1)/2 comparisons, because each element is compared against every element to its left. The input's ordering, not its size, causes the difference.