Imagine keeping track of the highest score you have seen in a stream of test results. Each new score takes one comparison. When it beats the current highest score, you update your record. The method is simple. Its number of comparisons is predictable, but its number of updates depends on the order in which the scores arrive.
Background for this page. Probability weights possible outcomes. An expectation is a weighted average; an individual run still performs a whole number of operations. Related explanations: Counting, probability, and expectation for algorithms · Quicksort: partitioning, pivots, and running time.
If the scores arrive as [2, 4, 6, 8], you set a new record four times. If they arrive as [8, 6, 4, 2], you set it once. The method is the same in both cases. The input order changes the cost of the expensive part.
| Order of scores | New records, in order | Updates |
|---|---|---|
2, 4, 6, 8 | 2, 4, 6, 8 | 4 |
8, 6, 4, 2 | 8 | 1 |
6, 2, 8, 4 | 6, 8 | 2 |
This is the idea behind the textbook’s hiring example. Interviewing every candidate has one cost. Replacing the current choice whenever a better candidate appears has another, larger cost. The scores here are only a small numerical model of that situation. They let us see how order changes the number of replacements without pretending that real hiring can be reduced to one number.
Random input and random choices
There are two different ways chance can enter the analysis. First, the algorithm may be fixed while the input order is random. We can then ask how many record changes to expect across all possible orders. That is probabilistic analysis of a deterministic method.
Second, the algorithm itself may make a random choice. We could shuffle the input before scanning it. Then the order we examine is random even if someone handed us the scores in increasing order. That makes the method randomized. It does not make every run have the same cost; it gives us a way to reason about the distribution of possible costs.
Here is a small Python version. It takes a random-number generator as an argument so a reader can pass a fixed seed when repeating an experiment:
import random
# Shuffle the scores, then count new records.
def count_record_changes(scores, rng):
# Keep the caller's list unchanged.
order = list(scores)
rng.shuffle(order)
# No score has been seen yet.
best = None
changes = 0
for score in order:
# Equal scores do not set a new record.
if best is None or score > best:
best = score
changes += 1
return order, changes
# A fixed seed makes this example repeatable.
rng = random.Random(7)
print(count_record_changes([2, 4, 6, 8], rng))
The function still examines every score, so its scanning work is linear in the number of scores. Shuffling also takes work. What varies is the number of record changes. If updating a record is expensive, this difference can matter. A fixed seed is useful for a repeatable demonstration, but repeating one result does not tell us the average across all possible orders.
What should we expect?
Suppose every ordering of n distinct scores is equally likely. At position 1, the score must be a new record. At position 2, the chance that the new score is the best of the first two is 1/2. At position i, any one of the first i scores is equally likely to be the largest, so the chance that the score at position i sets a new record is 1/i.
Let Xᵢ be 1 if position i sets a new record, and 0 otherwise. Its expected value is E[Xᵢ] = 1/i. The total number of changes is X₁ + X₂ + … + Xₙ. Expected values add, so the expected number of changes is
E[changes] = 1 + 1/2 + 1/3 + … + 1/n.
This sum is called the nth harmonic number. It grows roughly like ln n. We did not need the record events to be independent to add their expected values; that is one reason indicator variables are useful. If each update costs much more than a comparison, this tells us something valuable about the average cost under the random-order assumption. It says nothing about a guarantee for one particular run. The increasing order can still produce n updates.
The assumption matters. If the incoming order is chosen to be increasing, the random-order analysis does not describe it. Shuffling inside the algorithm gives us a random order, provided the shuffle is sufficiently uniform for the model we use. It also costs time and extra space in this version. We should count those costs when deciding whether randomization helps in a real program.
Randomized algorithms appear again in sorting, selection, and other parts of this course. They are useful because a random choice can prevent a fixed input pattern from repeatedly causing poor behavior. Their claims are usually about expected cost or a probability of an outcome, not a promise that every run is fast.
Check the expectation on three scores
Under a uniform random order of three distinct scores, the six orders have record-update counts 3, 2, 2, 2, 1, and 1. Their average is 11/6. The harmonic calculation gives the same result: 1+1/2+1/3. At position i, each of the first i values is equally likely to be their maximum, so the new value sets a record with probability 1/i. Adding these probabilities gives the expected count even though the record events depend on each other.
Practice. For the order [5, 1, 7, 3, 9], which values set a new record? If five distinct values arrive in a uniformly random order, what expression gives the expected number of record changes?
Answer. The new records are 5, 7, and 9, so there are three changes. Under the random-order assumption, the expected count is 1 + 1/2 + 1/3 + 1/4 + 1/5, about 2.28. That average is not the number of changes in every order.
We now have three useful ways to think about an algorithm: what it does, how its cost grows, and how its cost may vary. Next we will use those ideas to study sorting and selection more closely.
Try another case. Does an expectation of 11/6 mean a run makes a fractional update?
Reasoning. No. It is an average across possible orders, not an outcome promised for one run.