Best, worst and average case is a concept in computer science used to describe the resource usage of an algorithm under different conditions. For a given algorithm, these three cases express, respectively, the minimum, maximum, and average amount of a resource consumed. Although the resource considered is most commonly running time (i.e., time complexity), it can also refer to memory usage or other computational resources.
Definitions
- Best case refers to the scenario in which the algorithm performs the minimum number of steps on input data of n elements. For example, for a simple linear search on a list, the best case occurs when the desired element is the very first element of the list.
- Worst case refers to the scenario in which the algorithm performs the maximum number of steps on input data of size n. For a linear search, the worst case occurs when the target value is the last element of the list or is not present at all, requiring the algorithm to examine every element.
- Average case refers to the function that performs an average number of steps on input data of n elements. For linear search, assuming the sought value is in the list and each element is equally likely to be the target, the average number of elements visited is n/2.
Use in Algorithm Analysis
Average-case and worst-case performance are the two measures most frequently used in algorithm analysis. Worst-case analysis provides a guaranteed upper bound on resource usage and is particularly important in real-time computing, where the worst-case execution time must be known to ensure an algorithm always finishes in time. Best-case performance is used less often and is generally considered of limited analytical value, since a lower bound gives little insight when the worst-case running time can be very large.
Worst-case and average-case analyses typically require different tools and approaches. Determining what constitutes "typical" input is often difficult, and average input frequently has properties that complicate mathematical characterization. Worst-case analysis is considered "safe" because it never underestimates the resource requirement, but it can be overly pessimistic when no realistic input actually triggers the worst-case scenario.
Amortized analysis is a related technique that determines the worst-case running time over a (possibly infinite) series of operations, often yielding a value much closer to the average cost while still providing a guaranteed upper limit.
Examples
The three cases are commonly illustrated using sorting algorithms. For example:
- Quicksort has an average-case and best-case performance of O(n log n), but a worst-case performance of O(n²) for certain inputs.
- Merge sort and heapsort maintain O(n log n) performance in all three cases.
- Insertion sort has a best-case performance of O(n) (when the input is already sorted) and a worst- and average-case performance of O(n²).
For data structures, the best, average, and worst cases can differ substantially. Hash tables, for instance, typically offer O(1) average-case insertion and search operations but can degrade to O(n) in the worst case due to collisions.
Practical Consequences
Many algorithms with poor worst-case performance have good average-case performance. For most problems, this is desirable, as typical instances tend to behave closer to the average than to the worst case. In cryptography, however, this is problematic: cryptographic problems are designed so that typical instances are hard, and techniques such as random self-reducibility are used to show that the worst case is no harder than the average case.
The terms "best case" and "worst case" are also used in contexts beyond computer science, such as the best- and worst-case outcomes of an epidemic, or the worst-case temperature to which an electronic component may be exposed, where devices must operate correctly under worst-case combinations of tolerances and external conditions.