A Selection Algorithm that uses a similar technique to QuickSort in finding the
Evaluation
Runtime comes from same Time Complexity justification as BuildHeap
Pseudocode
See QuickSort for explanation of choose pivot and paritioning steps.
quickselect(array, start, end, k) {
if end - start <= 1 return
// Choose pivot
// put it out of the way for partitioning
pivotIndex := rand([start, end])
pivotVal := array[pivotIndex]
swap elements at start, pivotIndex
// Partitioning
// Move elements larger than the pivot to the right side of the array
// Move elements smaller than the pivot to the left side of the array
// Not crossed -> i <= j
i := start + 1
j := end
while i and j have not crossed position {
while i and j in not crossed position and array[i] <= pivotVal { i++ }
while i and j in not crossed position and array[j] >= pivotVal { j-- }
if i and j have not crossed position {
swap elements at i, j
i++
j--
}
}
// quickselect special behavior vs quick sort
swap elements at start, j
if j == k - 1 { return array[j] } // kth value has been found
if j > k - 1 { quickselect(array, start, j - 1, k) } // recurse left
else { quickselect(array, j + 1, end, k) } // recurse right
}