선택 알고리즘
The name shown is in ko. The article below is in English and is not a translation.
≈ 16 min read
Names in other languages
- УкраїнськаПошук порядкової статистики
- FrançaisAlgorithme de sélection
- EspañolAlgoritmo de SelecciónAlgoritmo de Seleccion
- caalgorisme de selecció
- Portuguêsalgoritmo de seleção
- Portuguêsalgoritmo de seleção
- Românăalgoritm de selecție
- СрпскиСелекциони алгоритам
- Englishselection algorithm
- العربيةخوارزمية الاختيار
- fivalinta-algoritmi
- Bahasa IndonesiaAlgoritma seleksi
- slizbirni algoritem
- Polskiproblem selekcji
- فارسیالگوریتم انتخاب
- РусскийАлгоритм выбора
- עבריתאלגוריתם בחירה
- TürkçeSeçim problemi
- DeutschSuchverfahren
- Magyarkiválasztás
- 日本語選択アルゴリズム
- 한국어선택 알고리즘
- 中文选择算法
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer science, a selection algorithm is an algorithm for finding the k th smallest value in a collection of orderable values, such as numbers. The value that it finds is called the 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 values, these algorithms take linear time, 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
In computer science, a selection algorithm is an algorithm for finding the k th smallest value in a collection of orderable values, such as numbers. The value that it finds is called the 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 values, these algorithms take linear time, 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 ==Problem statement== An algorithm for the selection problem takes as input a collection of values, and a It outputs the smallest of these values, or, in some versions of the problem, a collection of the 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 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 as in zero-based numbering of arrays, or is it obtained by 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 With these conventions, the maximum value, among a collection of n values, is obtained by When n is an odd number, the median of the collection is obtained by When n is even, there are two choices for the median, obtained by rounding this choice of k down or up, respectively: the lower median with k=n/2 and the upper median with ==Algorithms== ===Sorting and heapselect=== As a baseline algorithm, selection of the smallest value in a collection of values can be performed by the following two steps: Sort the collection If the output of the sorting algorithm is an array, retrieve its element; otherwise, scan the sorted sequence to find the element. The time for this method is dominated by the sorting step, which requires \Theta(n\log n) time using a Even when integer sorting algorithms may be used, these are generally slower than the linear time that may be achieved using specialized selection algorithms. Nevertheless, the simplicity of this approach makes it attractive, especially when a highly-optimized sorting routine is provided as part of a runtime library, but a selection algorithm is not. For inputs of moderate size, sorting can be faster than non-random selection algorithms, because of the smaller constant factors in its running This method also produces a sorted version of the collection, which may be useful for other later computations, and in particular for selection with other choices For a sorting algorithm that generates one item at a time, such as selection sort, the scan can be done in tandem with the sort, and the sort can be terminated once the element has been found. One possible design of a consolation bracket in a single-elimination tournament, in which the teams who lost to the eventual winner play another mini-tournament to determine second place, can be seen as an instance of this Applying this optimization to heapsort produces the heapselect algorithm, which can select the smallest value in This is fast when k is small relative but degenerates to O(n\log n) for larger values such as the choice k=n/2 used for median finding. ===Pivoting=== Many methods for selection are based on choosing a special "pivot" element from the input, and using comparisons with this element to divide the remaining n-1 input values into two subsets: the set L of elements less than the pivot, and the set R of elements greater than the pivot. The algorithm can then determine where the smallest value is to be found, based on a comparison of k with the sizes of these sets. In particular, the smallest value is and can be found recursively by applying the same selection algorithm then the smallest value is the pivot, and it can be returned immediately. In the remaining case, the smallest value is and more specifically it is the element in position k-|L|-1 It can be found by applying a selection algorithm recursively, seeking the value in this position As with the related pivoting-based quicksort algorithm, the partition of the input into L and R may be done by making new collections for these sets, or by a method that partitions a given list or array data type in-place. Details vary depending on how the input collection is The time to compare the pivot against all the other values However, pivoting methods differ in how they choose the pivot, which affects how big the subproblems in each recursive call will be. The efficiency of these methods depends greatly on the choice of the pivot. If the pivot is chosen badly, the running time of this method can be as slow If the pivot were exactly at the median of the input, then each recursive call would have at most half as many values as the previous call, and the total times would add in a geometric series However, finding the median is itself a selection problem, on the entire original input. Trying to find it by a recursive call to a selection algorithm would lead to an infinite recursion, because the problem size would not decrease in each Quickselect chooses the pivot uniformly at random from the input values. It can be described as a prune and search algorithm, a variant of quicksort, with the same pivoting strategy, but where quicksort makes two recursive calls to sort the two subcollections L quickselect only makes one of these two calls. Its expected time For any constant C , the probability that its number of comparisons exceeds Cn is superexponentially small The Floyd–Rivest algorithm, a variation of quickselect, chooses a pivot by randomly sampling a subset of r data values, for some sample and then recursively selecting two elements somewhat above and below position rk/n of the sample to use as pivots. With this choice, it is likely that k is sandwiched between the two pivots, so that after pivoting only a small number of data values between the pivots are left for a recursive call. This method can achieve an expected number of comparisons that is In their original work, Floyd and Rivest claimed that the o(n) term could be made as small as O(\sqrt n) by a recursive sampling scheme, but the correctness of their analysis has been Instead, more rigorous analysis has shown that a version of their algorithm achieves O(\sqrt{n\log n}) for this Although the usual analysis of both quickselect and the Floyd–Rivest algorithm assumes the use of a true random number generator, a version of the Floyd–Rivest algorithm using a pseudorandom number generator seeded with only logarithmically many true random bits has been proven to run in linear time with high probability. method. Each set of five elements is shown as a column of dots in the figure, sorted in increasing order from top to bottom. If their medians (the green and purple dots in the middle row) are sorted in increasing order from left to right, and the median of medians is chosen as the pivot, then the 3n/10 elements in the upper left quadrant will be less than the pivot, and the 3n/10 elements in the lower right quadrant will be greater than the pivot, showing that many elements will be eliminated by pivoting. The median of medians method partitions the input into sets of five elements, and uses some other non-recursive method to find the median of each of these sets in constant time per set. It then recursively calls itself to find the median of these n/5 medians. Using the resulting median of medians as the pivot produces a partition with Thus, a problem on n elements is reduced to two recursive problems on n/5 elements (to find the pivot) and at most 7n/10 elements (after the pivot is used). The total size of these two recursive subproblems is at allowing the total time to be analyzed as a geometric series adding Unlike quickselect, this algorithm is deterministic, not It was the first linear-time deterministic selection algorithm and is commonly taught in undergraduate algorithms classes as an example of a divide and conquer that does not divide into two equal However, the high constant factors in its O(n) time bound make it slower than quickselect in and slower even than sorting for inputs of moderate Hybrid algorithms such as introselect can be used to achieve the practical performance of quickselect with a fallback to medians of medians guaranteeing worst-case O(n) ===Factories=== The deterministic selection algorithms with the smallest known numbers of comparisons, for values of k that are far from 1 are based on the concept of factories, introduced in 1976 by Arnold Schönhage, Mike Paterson, and These are methods that build partial orders of certain specified types, on small subsets of input values, by using comparisons to combine smaller partial orders. As a very simple example, one type of factory can take as input a sequence of single-element partial orders, compare pairs of elements from these orders, and produce as output a sequence of two-element totally ordered sets. The elements used as the inputs to this factory could either be input values that have not been compared with anything yet, or "waste" values produced by other factories. The goal of a factory-based algorithm is to combine together different factories, with the outputs of some factories going to the inputs of others, in order to eventually obtain a partial order in which one element (the smallest) is larger than some k-1 other elements and smaller than another n-k others. A careful design of these factories leads to an algorithm that, when applied to median-finding, uses at most 2.942n comparisons. For other values the number of comparisons is ===Parallel algorithms=== Parallel algorithms for selection have been studied since 1975, when Leslie Valiant introduced the parallel comparison tree model for analyzing these algorithms, and proved that in this model selection using a linear number of comparisons requires \Omega(\log\log n) parallel steps, even for selecting the minimum or Researchers later found parallel algorithms for selection in O(\log\log n) steps, matching this In a randomized parallel comparison tree model it is possible to perform selection in a bounded number of steps and a linear number of On the more realistic parallel RAM model of computing, with exclusive read exclusive write memory access, selection can be performed in time O(\log n) with O(n/\log n) processors, which is optimal both in time and in the number of With concurrent memory access, slightly faster parallel time is possible in and the \log n term in the time bound can be replaced ===Sublinear data structures=== When data is already organized into a data structure, it may be possible to perform selection in an amount of time that is sublinear in the number of values. As a simple case of this, for data already sorted into an array, selecting the element may be performed by a single array lookup, in constant For values organized into a two-dimensional array of with sorted rows and columns, selection may be performed in time or faster when k is small relative to the array For a collection of m one-dimensional sorted arrays, with k_i items less than the selected item in the array, the time is Selection from data in a binary heap takes This is independent of the size n of the heap, and faster than the O(k\log n) time bound that would be obtained from This same method can be applied more generally to data organized as any kind of heap-ordered tree (a tree in which each node stores one value in which the parent of each non-root node has a smaller value than its child). This method of performing selection in a heap has been applied to problems of listing multiple solutions to combinatorial optimization problems, such as finding the shortest paths in a weighted graph, by defining a state space of solutions in the form of an implicitly defined heap-ordered tree, and then applying this selection algorithm to this In the other direction, linear time selection algorithms have been used as a subroutine in a priority queue data structure related to the heap, improving the time for extracting its item from O(\log n) to here \log^* n is the For a collection of data values undergoing dynamic insertions and deletions, the order statistic tree augments a self-balancing binary search tree structure with a constant amount of additional information per tree node, allowing insertions, deletions, and selection queries that ask for the element in the current set to all be performed in O(\log n) time per Going beyond the comparison model of computation, faster times per operation are possible for values that are small integers, on which binary arithmetic operations are It is not possible for a streaming algorithm with memory sublinear in both n and k to solve selection queries exactly for dynamic data, but the count–min sketch can be used to solve selection queries approximately, by finding a value whose position in the ordering of the elements (if it were added to them) would be within \varepsilon n steps of k , for a sketch whose size is within logarithmic factors of 1/\varepsilon . For deterministic algorithms, it has been shown that selecting the element requires \bigl(1+H(k/n)\bigr)n+\Omega(\sqrt n) comparisons, where H(x)=x\log_2\frac1x + (1-x)\log_2\frac1{1-x} is the The special case of median-finding has a slightly larger lower bound on the number of comparisons, for of the order relations found so far (with smaller=lower and larger=higher) as blue line segments. The red elements have already been found to be greater than three others and so cannot be the median. The larger of the two elements in the final comparison is the median. Knuth supplies the following triangle of numbers summarizing pairs of n and k for which the exact number of comparisons needed by an optimal selection algorithm is known. The row of the triangle (starting with n=1 in the top row) gives the numbers of comparisons for inputs of n values, and the number within each row gives the number of comparisons needed to select the smallest value from an input of that size. The rows are symmetric because selecting the smallest requires exactly the same number of comparisons, in the worst case, as selecting the Most, but not all, of the entries on the left half of each row can be found using the formula n-k+(k-1)\bigl\lceil\log_2(n+2-k)\bigr\rceil. This describes the number of comparisons made by a method of Abdollah Hadian and Milton Sobel, related to heapselect, that finds the smallest value using a single-elimination tournament and then repeatedly uses a smaller tournament among the values eliminated by the eventual tournament winners to find the next successive values until reaching the smallest. Some of the larger entries were proven to be optimal using a computer search. == History== Quickselect was presented without analysis by Tony Hoare and first analyzed in a 1971 technical report by The first known linear time deterministic selection algorithm is the median of medians method, published in 19731 by Manuel Blum, Robert W. Floyd, Vaughan Pratt, Ron Rivest, and Robert Tarjan. They trace the formulation of the selection problem to work of Charles L. Dodgson (better known as Lewis Carroll) who in 1883 pointed out that the usual design of single-elimination sports tournaments does not guarantee that the second-best player wins second place, and to work of Hugo Steinhaus circa 1930, who followed up this same line of thought by asking for a tournament design that can make this guarantee, with a minimum number of games played (that is,
Text derived from https://en.wikipedia.org/w/index.php?oldid=1373095928 under CC BY-SA 4.0; modifications have been made.