Choose the median value among the first, middle and last entries of an active array segment. For at least three distinct sampled values, the selected value cannot be the whole segment's minimum or maximum. It can still be the second-smallest or second-largest value, so a balanced split does not follow. Coincident sample indices in segments of length one or two can be handled by the same comparison code. Ties must also have a defined branch policy.
A distinct-input counterexample for Quickselect that chooses the first/middle/last median, reserves it at the last slot, scans lower values to the front, and finally swaps the pivot into place. Let be the following permutation of . For , take increasing order. For , let componentwise and , then set
and put at every other index . These assignments use every entry of once and add zero and one, so they really define a permutation.
The sampled values are , with the last at least two, so the pivot is one. After the initial pivot swap, the only lower value is the zero at index . Moving it to the front places the displaced at its intended tail position. The final pivot swap moves from index one to the last position. Every other tail entry already has its intended position. Thus the complete partitioned result is exactly followed by .
Selecting the maximum consequently repeats the same construction on entries. If counts only pivot comparisons made by the partition scan, then , with and . Hence and total runtime is . For example, . This disproves a linear worst-case claim even when every value is distinct.

Articles by others on the same topic (0)

There are currently no matching articles.