Best, worst, and average
After thiswhat you will be able to doClassify the best, worst, and uniform-average costs of a front-to-back search with O, Omega, and Theta without confusing the case with the bound.
Questionwhat this lesson answersTwo earlier lessons said the growth bounds and the choice of input are different axes, and one built the average. So what are the three cases, how does each combine with an upper, lower, or tight bound, and why does asking for "the complexity" of an algorithm have no single answer?
Not coveredwhat this lesson leaves outWe fill the grid of three cases against three bounds for one search, using the average the expectation lesson already computed. We do not survey the cases of any harder algorithm, and we do not prove a lower bound over all algorithms.
Two threads have been running beside each other. The Upper, lower, and tight lesson said that a bound describes how one chosen rule for cost changes as the input gets larger. gives that rule a ceiling, gives it a floor, and says the ceiling and floor fit the same growth shape. That lesson also pointed out that best, worst, and average do something else: they choose which input of a given size supplies the cost. A bound does not make that choice.
The Expectation lesson took the second thread and built one average. Its front-to-back search checks a list from the first item onward. When every target position is chosen uniformly, the weighted average is comparisons. That calculation belongs behind the Expectation door. Here we take its result as given and place it beside the two extreme cases.
One search, three ways to choose an input
Keep the algorithm concrete. A front-to-back search looks at each item until it reaches the target. Let be the number of items in the list. If the target sits at position , the search makes comparisons, because it checks every earlier item and then that position. The target first gives one comparison. The target last gives one comparison for every item.
Choosing the first target is the best case. Choosing the final target is the worst case. Choosing among all positions with the uniform rule gives the average case already computed by expectation. For this one search, the three costs are:
These are not three names for one number. They are three different answers to the question, “which input of size are we measuring?” The average answer needs its uniform assumption. Without that assumption, the search still has a best and a worst cost, but it does not have this particular average.
Choose the case first, then the bound
The central move is an order of questions. First choose a case. That gives one rule that tells us the comparison count for each list size. A rule of that kind is called a cost function. Then put a bound on that chosen function. The case and the bound are independent choices, so they make a grid: rows choose inputs, and columns describe the resulting cost.
Start with the best row. Its cost is the constant . It never increases with , so it has an upper bound and a lower bound . Since both use the same constant shape, the row also has the tight bound .
The worst row is just . It has an upper bound , a lower bound , and the tight description . The three labels do not compete. They give a ceiling, floor, and tight fit for the same worst-case cost function.
The average row is not constant just because it is smaller than the worst row. Its cost is . Half a list still grows in step with the whole list, which is the linear shape. The two simple inequalities show why both the ceiling and floor use :
So the uniform average is , , and . The tight label is available in every row of this example because we know the exact shape of every row’s cost. Another algorithm might leave us with only a ceiling or only a floor until more work is done.
Best case: one comparison
When the target is first, the search stops after one comparison. Its ceiling and its floor both use the constant shape, so the tight fit is Theta of one.
Choose a case, then a bound
The rows choose inputs. The columns describe growth.
Each revealed row is one way to choose an input of the same size. The columns then put a ceiling, a floor, and a tight fit on that row's cost.
| Case | Cost rule | O ceiling | Omega floor | Theta tight fit |
|---|---|---|---|---|
| Best case | 1 | O(1) | Omega(1) | Theta(1) |
- Best case comparisons
- 1
- Worst case comparisons
- 8
- Average case comparisons
- 4.5
For 8 items, the three cases cost 1, 8, and 4.5 comparisons.
The slider changes the three actual comparison counts, while the table keeps the two axes apart. The best count stays at one. The average stays between the best and worst counts. Its tight shape still matches the worst row’s shape even though its count is smaller at every list size above one.
The missing word in “the complexity”
A compact claim about a program’s cost as its input grows is called a complexity statement. The phrase “the complexity of the search” is not one statement. It needs at least a case and a bound. Someone who says “the search is ” has often quietly chosen the worst case. The uniform average also has that upper bound. Someone who says “it is ” has quietly chosen the best case. Neither statement is wrong, but neither identifies the search’s behavior completely without naming its case.
That is why one algorithm carries several honest complexity statements at once. The same front-to-back search is best-case , worst-case , and uniform-average . Saying the case out loud says which cost function the bound is about. Saying the bound says what kind of claim is being made about that cost function.
Worst case and are often spoken together, but they answer different questions. Worst case chooses the input that makes this search work the longest. asks for a ceiling on whichever cost has already been chosen. The grid gives a lower bound on the best case just as easily. The pairing “worst case, O” is a convention, not a law.
There is one final limit to keep visible. A lower bound on this algorithm’s worst case says this algorithm cannot do better than that cost on its worst inputs. It does not say that no algorithm can do better on the same problem. That stronger statement needs a different method, behind the Lower bound proofs door.
Doorswhat to read next, and why
- Upper, lower, and tightThis lesson places the three cases against the three growth bounds. The bounds themselves, with their witnesses, are built there.
- ExpectationThe average case used here is the weighted sum that lesson defines. This lesson reuses its result rather than rebuilding it.
- Lower bound proofsA lower bound on one algorithm's worst case is not a lower bound over every algorithm. That harder claim is a separate method.
Symbolswhat each one means, and whether we defined it, measured it, or just started there
- nStatus: defined
- the number of items in the list being searched
- kStatus: defined
- the position of the target, which is the number of comparisons the front-to-back search makes to reach it
- a caseStatus: defined
- a rule for choosing which input of a given size to measure, here best, worst, or average
- a boundStatus: door
- one of O, Omega, or Theta, a statement comparing a chosen cost with a growth shape
What these classifications mean
- defined
- circular by construction, true because we chose it
- empirical
- a measured claim about the world that could have come out otherwise
- bottoms out
- a primitive of the model, with nothing under it here
- door
- used here, explained elsewhere