Best and Worst Cases for One Input Size
Best and worst cases compare complete inputs of the same size and report the smallest or largest cost under a specified model.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Find two witnesses
A scan of five entries for a target needs one equality check when the target is first. It can take five when the target is absent. Both lists can have the same length. The stopping rule for a first match explains the difference.
Avoid mixing input sizes
Comparing an early match in a long list to a late match in a short list gives particular costs, but neither proves a growth rate. Counting operations defines the quantity to maximize at each length.[1]
Construct inputs that attain both extremes
An insertion sort can compare a new key with earlier sorted keys until it finds the point where shifting stops. On four keys already in increasing order, one key comparison per insertion suffices under the usual straight insertion loop. In decreasing order every earlier key must shift. See sorted insertion costs.
Increasing 1,2,3,4: comparisons 1+1+1 = 3
Decreasing 4,3,2,1: comparisons 1+2+3 = 6
Shifts respectively 0 and 6
Identify the algorithm and input size in both claims
Different implementations count a boundary check separately or use binary search for the insertion position. State whether you count only key comparisons. A worst-case argument needs both an example reaching the bound and reasoning that no other input of that size exceeds it.