Best, worst and average case


In computer science, best, worst, and average cases of a given algorithm express what the resource usage is at least, at most and on average, respectively. Usually the resource being considered is running time, i.e. time complexity, but could also be memory or other resource.
Best case is the function which performs the minimum number of steps on input data of n elements.
Worst case is the function which performs the maximum number of steps on input data of size n.
Average case is the function which performs an average number of steps on input data of n elements.
In real-time computing, the worst-case execution time is often of particular concern since it is important to know how much time might be needed in the worst case to guarantee that the algorithm will always finish on time.
Average performance and worst-case performance are the most used in algorithm analysis. Less widely found is best-case performance, but it does have uses: for example, where the best cases of individual tasks are known, they can be used to improve the accuracy of an overall worst-case analysis. Computer scientists use probabilistic analysis techniques, especially expected value, to determine expected running times.
The terms are used in other contexts; for example the worst- and best-case outcome of a planned-for epidemic, worst-case temperature to which an electronic circuit element is exposed, etc. Where components of specified tolerance are used, devices must be designed to work properly with the worst-case combination of tolerances and external conditions.

Best case performance for algorithm

The term best-case performance is used in computer science to describe an algorithm's behavior under optimal conditions. For example, the best case for a simple linear search on a list occurs when the desired element is the first element of the list.
Development and choice of algorithms is rarely based on best-case performance: most academic and commercial enterprises are more interested in improving Average-case complexity and worst-case performance. Algorithms may also be trivially modified to have good best-case running time by hard-coding solutions to a finite set of inputs, making the measure almost meaningless.

Worst-case versus average-case performance

Worst-case performance analysis and average-case performance analysis have some similarities, but in practice usually require different tools and approaches.
Determining what typical input means is difficult, and often that average input has properties which make it difficult to characterise mathematically. Similarly, even when a sensible description of a particular "average case" is possible, they tend to result in more difficult analysis of equations.
Worst-case analysis gives a safe analysis, but one which can be overly pessimistic, since there may be no input that would take this many steps.
In some situations it may be necessary to use a pessimistic analysis in order to guarantee safety. Often however, a pessimistic analysis may be too pessimistic, so an analysis that gets closer to the real value but may be optimistic can be a much more practical approach. One modern approach in academic theory to bridge the gap between worst-case and average-case analysis is called smoothed analysis.
When analyzing algorithms which often take a small time to complete, but periodically require a much larger time, amortized analysis can be used to determine the worst-case running time over a series of operations. This amortized worst-case cost can be much closer to the average case cost, while still providing a guaranteed upper limit on the running time.
The worst-case analysis is related to the worst-case complexity.

Practical consequences

Many algorithms with bad worst-case performance have good average-case performance. For problems we want to solve, this is a good thing: we can hope that the particular instances we care about are average. For cryptography, this is very bad: we want typical instances of a cryptographic problem to be hard. Here methods like random self-reducibility can be used for some specific problems to show that the worst case is no harder than the average case, or, equivalently, that the average case is no easier than the worst case.
On the other hand, some data structures like hash tables have very poor worst case behaviors, but a well written hash table of sufficient size will statistically never give the worst case; the average number of operations performed follows an exponential decay curve, and so the run time of an operation is statistically bounded.

Examples

Sorting algorithms

AlgorithmData structureTime complexity:BestTime complexity:AverageTime complexity:WorstSpace complexity:Worst
Quick sortArrayOOOO
Merge sortArrayOOOO
Heap sortArrayOOOO
Smooth sortArrayOOOO
Bubble sortArrayOOOO
Insertion sortArrayOOOO
Selection sortArrayOOOO
Bogo sortArrayOOOO