In computer science, a selection algorithm is an algorithm for finding the k {\displaystyle k} th smallest value in a collection of orderable values, such as numbers. The value that it finds is called the k {\displaystyle k} th order statistic. Selection includes as special cases the problems of finding the minimum, median, and maximum element in the collection. Selection algorithms include quickselect, and the median of medians algorithm. When applied to a collection of n {\displaystyle n} values, these algorithms take linear time, O ( n ) {\displaystyle O(n)} as expressed using big O notation. For data that is already structured, faster algorithms may be possible; as an extreme case, selection in an already-sorted array takes time O ( 1 ) {\displaystyle O(1)} .
Problem statement An algorithm for the selection problem takes as input a collection of values, and a number k {\displaystyle k} . It outputs the k {\displaystyle k} th smallest of these values, or, in some versions of the problem, a collection of the k {\displaystyle k} smallest values. The latter version can be further specified as some versions output the k smallest values in sorted order, while others return them in arbitrary order. For this to be well-defined, it should be possible to sort the values into an order from smallest to largest; for instance, they may be integers, floating-point numbers, or some other kind of object with a numeric key. However, they are not assumed to have been already sorted. Often, selection algorithms are restricted to a comparison-based model of computation, as in comparison sort algorithms, where the algorithm has access to a comparison operation that can determine the relative ordering of any two values, but may not perform any other kind of arithmetic operations on these values. To simplify the problem, some works on this problem assume that the values are all distinct from each other, or that some consistent tie-breaking method has been used to assign an ordering to pairs of items with the same value as each other. Another variation in the problem definition concerns the numbering of the ordered values: is the smallest value obtained by setting k = 0 {\displaystyle k=0} , as in zero-based numbering of arrays, or is it obtained by setting k = 1 {\displaystyle k=1} , following the usual English-language conventions for the smallest, second-smallest, etc.? This article follows the conventions used by Cormen et al., according to which all values are distinct and the minimum value is obtained from k = 1 {\displaystyle k=1} . With these conventions, the maximum value, among a collection of n {\displaystyle n} values, is obtained by setting k = n {\displaystyle k=n} . When n {\displaystyle n} is an odd number, the median of the collection is obtained by setting k = ( n + 1 ) / 2 {\displaystyle k=(n+1)/2} . When n {\displaystyle n} is even, there are two choices for the median, obtained by rounding this choice of k {\displaystyle k} down or up, respectively: the lower median with k = n / 2 {\displaystyle k=n/2} and the upper median with k = n / 2 + 1 {\displaystyle k=n/2+1} .
Algorithms
Sorting and heapselect As a baseline algorithm, selection of the k {\displaystyle k} th smallest value in a collection of values can be performed by the following two steps:
… excerpt ends here. Continue reading the full article.


