ArticleslgStudy

computer science

Selection algorithm

Selection algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Selection algorithm rather than just read about it. In short: In computer science, a selection algorithm is an algorithm for finding the kth smallest value in a collection of orderable values, such as numbers. The value that it finds is called the kth order statistic.

Selection algorithm — main illustration
Selection algorithm — illustration

Key takeaways

  • Selection algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Selection algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Selection algorithm from memory before moving on to harder problems.

Reference excerpt

In computer science, a selection algorithm is an algorithm for finding the kth smallest value in a collection of orderable values, such as numbers. The value that it finds is called the kth 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 time O(1).

Problem statement An algorithm for the selection problem takes as input a collection of values, and a number k. It outputs the kth 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 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, as in zero-based numbering of arrays, or is it obtained by setting 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. With these conventions, the maximum value, among a collection of n values, is obtained by setting k=n. When n is an odd number, the median of the collection is obtained by setting k=(n+1)/2. 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 k=n/2+1.

Algorithms

Sorting and heapselect As a baseline algorithm, selection of the kth 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 kth element; otherwise, scan the sorted sequence to find the kth element. The time for this method is dominated by the sorting step, which requires Θ(nlog⁡n) time using a comparison sort. 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 time. 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 of k. 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 kth 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 method. Applying this optimization to heapsort produces the heapselect algorithm, which can select the kth smallest value in time O(n+klog⁡n). This is fast when k is small relative to n, but degenerates to O(nlog⁡n) for larger values of k, 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 kth smallest value is to be found, based on a comparison of k with the sizes of these sets. In particular, if k≤|L|, the kth smallest value is in L, and can be found recursively by applying the same selection algorithm to L. If k=|L|+1, then the kth smallest value is the pivot, and it can be returned immediately. In the remaining case, the kth smallest value is in R, and more specifically it is the element in position k−|L|−1 of R. It can be found by applying a selection algorithm recursively, seeking the value in this position in R. 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 represented. The time to compare the pivot against all the other values is O(n). 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 as O(n2).

… excerpt ends here. Continue reading the full article.

Illustrations

Selection algorithm: Finding the median of five values using six comparisons. Each step shows the comparisons to be performed next as yellow line segments, and a Hasse diagram 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.
Finding the median of five values using six comparisons. Each step shows the comparisons to be performed next as yellow line segments, and a Hasse diagram 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.

Worked examples

Example 1 — a first encounter with Selection algorithm

Start with the simplest possible case. Write down what Selection algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Selection algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Selection algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Selection algorithm

In research
Selection algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Selection algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Selection algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Selection algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Selection algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Selection algorithm” →

How to study Selection algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Selection algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Selection algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Selection algorithm in simple terms?

In computer science, a selection algorithm is an algorithm for finding the kth smallest value in a collection of orderable values, such as numbers. The value that it finds is called the kth order statistic.

Why does Selection algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Selection algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Selection algorithm.

Tags

  • Selection algorithms

Keep exploring