Conceptintro
Best-case analysis
Also called best-case running time.
Gives the minimum running time over all inputs of a given size.
Best-case analysis measures the running time of an algorithm as the smallest time over all inputs of the same size. The result is a function . It is a lower bound on the cost of the algorithm on every input, and it is not a promise of speed.
In plain words
An algorithm can be fast on one input and slow on another input of the same size. The best case asks for the fastest case. If you look for a name in a list and the name is first, you stop at once. That is the best case for the list, and it says little about the lists that you will meet.
The best case is rarely the number that you want. It gives no forecast and no promise of speed. It is also easy to fake. Take any slow method and add a quick test for one special input. The new method has a fast best case, and it is not better in any other way.
The best case still has some uses. It shows which inputs make a method fast. It also shows that no input can be faster than a given amount.
The idea
For an algorithm (see time complexity) on a set of inputs of size , with the number of steps on input in a stated model:
The minimum exists when takes values in the natural numbers and is not empty. For every distribution on , , with the worst case on the right and the average case in the middle.
To prove , give one input of size with . To prove , show for every . This is the opposite of the worst case, where the upper bound needs all inputs and the lower bound needs one input. A statement such as (see asymptotic notation) is a lower bound for every input.
Why it is rarely useful
- It is not a guarantee. A system that has to meet a deadline needs a bound that holds for the inputs it receives.
- It is often tiny and far from typical. A lucky input can be a sorted array, a key in the first place or an empty graph.
- It does not compare algorithms well. An algorithm with a fast best case can be slower than another on every input that occurs in practice.
The trap: a special check
Take any correct algorithm and, for each , one fixed input with a known answer . Define : compare the input with , and if they are equal, output ; else run . The best case of is the cost of recognizing . To accept , the check must read all of , so the best case of is at least the cost of reading . It does not depend on how good is. The worst case of grows by at most the cost of the check.
For sorting, take to be the sorted order. Test whether the array is sorted: this takes at most comparisons, and exactly when it is sorted. Then any sorting algorithm, even a slow one, gets a best case of comparisons, and its worst case grows by at most comparisons. So a claim "best case " tells you about the algorithm only if the algorithm is built to be fast on a wide class of inputs, as in adaptive sorting, and not by a check for one input.
Use of the best case
- Which inputs are fast. The best case names the inputs of the fast path. For insertion sort it is the sorted input, and the cost grows with the disorder of the input, as in an adaptive sort.
- Equal best and worst case. If , the cost does not depend on the data. Selection sort makes comparisons on every input of size .
- A lower bound for every input. If an algorithm reads all its input, then , and every input takes at least linear time.
- A check on a claimed lower bound. The lower bound for sorting, comparisons, is a worst-case bound for the problem. It does not apply to the best case of one algorithm: insertion sort makes comparisons on sorted input, which is fewer than for (for : 7 against 16).
The notations , and are about functions and not about cases. "The best case of insertion sort is " and "the best case of insertion sort is " are both correct and say . See asymptotic notation.
Examples
- Linear search. The best case is a key in the first place: 1 comparison, for each . The worst case is comparisons.
- Binary search. With a three-way comparison at each probe, the best case is a key at the middle probe: 1 probe. The worst case is probes. The variant that leaves the equality test to the end always makes about probes.
- Insertion sort. For the key is compared with at least one item of the sorted part, so there are at least comparisons. A sorted array, in which each key is not smaller than the item before it, gives exactly one comparison for each . So comparisons. For this is 7, and the worst case is .
- Bubble sort with an early exit. If a pass makes no swap, the algorithm stops. On a sorted array the first pass makes comparisons and no swap, so . In the usual version without the early exit, the count is on every input.
- Top-down merge sort. A merge of two sorted runs with and items makes at least comparisons: each comparison moves one item to the output, and when one run is empty the other is copied without comparisons, so all items of the run that empties first were output by comparisons. This bound is reached when all items of the shorter run are smaller than all items of the longer one. If the array is split into runs of and items, the best case obeys with . An input with distinct keys that reaches for every exists: put the smallest values in the right half and the others in the left half, and build each half in the same way. For equal to a power of 2 the sorted array reaches it, and . Proof by induction: and . So , against 17 comparisons in the worst case. The best case is about half of the worst case, and both are : here the best case does not change the order of growth.
- Non-example. Quicksort with the last item as pivot takes time on a sorted array, but this is the worst case and not the best case. The best case of quicksort is a pivot that splits the range in halves at every step, with comparisons.
Where it is used
Best-case bounds appear mainly as the lower end of a range, as in "insertion sort takes between and comparisons". They explain adaptive algorithms, whose cost depends on a property of the input such as the number of inversions. They give the cost of the fast path in a system that tests for a common case first. A worst-case or average-case bound is the right tool for a promise or a forecast. For a study of the inputs between the two extremes, see average-case analysis.
References
- https://en.wikipedia.org/wiki/Best,_worst_and_average_case, article Best, worst and average case (definition of the best case)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Section 2.2 (analysis of insertion sort, best case)
- Robert Sedgewick and Kevin Wayne, Algorithms, fourth edition, Section 1.4 (Analysis of Algorithms) and Section 2.1 (insertion sort)
- Donald E. Knuth, The Art of Computer Programming, Volume 3, second edition, Section 5.2.1 (sorting by insertion)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 5TGWDS Edit this page History Report an error Markdown JSON