MINIMUM(A) A: An unordered array
- min ← A[0]
- for i ← 1 to A.size-1 do
- if min > A[i]
- min = A[i]
- return min
This could be considered a tournment among the values
And each value but the min must lose once
So we need n-1 comparisons to determine the min/max in the worst case
So this is the best we can do.
SELECT(A,i) A: an unordered array i: an integer 0 ≤ i < A.size
- B = sort(A)
- return B[i]
PARTITION-SELECT(A, i, low, high)
A: an unordered array
i: an integer 0 ≤ i < A.size
low and high, valid positions in the array such that low ≤ i ≤ high
- if low = high
- return A[low]
- part ← Partition(A, low, high)
- if i == part
- return A[part]
- if i < part
- return PARTITION_SELECT(A, i, low, part-1)
- else
- return PARTITION_SELECT(A, i, part+1, high)