ArticleslgStudy

computer science

Quickselect

Quickselect 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 Quickselect rather than just read about it. In short: In computer science, quickselect is a selection algorithm to find the kth smallest element in an unordered list, also known as the kth order statistic. Like the related quicksort sorting algorithm, it was developed by Tony Hoare, and thus is also known as Hoare's selection algorithm.

Quickselect — main illustration
Quickselect — illustration

Key takeaways

  • Quickselect 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 Quickselect to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Quickselect from memory before moving on to harder problems.

Reference excerpt

In computer science, quickselect is a selection algorithm to find the kth smallest element in an unordered list, also known as the kth order statistic. Like the related quicksort sorting algorithm, it was developed by Tony Hoare, and thus is also known as Hoare's selection algorithm. Like quicksort, it is efficient in practice and has good average-case performance, but has poor worst-case performance. Quickselect and its variants are the selection algorithms most often used in efficient real-world implementations. Quickselect uses the same overall approach as quicksort, choosing one element as a pivot and partitioning the data in two based on the pivot, accordingly as less than or greater than the pivot. However, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side – the side with the element it is searching for. This reduces the average complexity from O ( n log ⁡ n ) {\displaystyle O(n\log n)} to O ( n ) {\displaystyle O(n)} , with a worst case of O ( n 2 ) {\displaystyle O(n^{2})} . As with quicksort, quickselect is generally implemented as an in-place algorithm, and beyond selecting the kth element, it also partially sorts the data. See selection algorithm for further discussion of the connection with sorting.

Algorithm In quicksort, there is a subprocedure called partition that can, in linear time, group a list (ranging from indices left to right) into two parts: those less than a certain element, and those greater than or equal to the element. Here is pseudocode that performs a partition about the element list[pivotIndex]:

function partition(list, left, right, pivotIndex) is pivotValue := list[pivotIndex] swap list[pivotIndex] and list[right] // Move pivot to end storeIndex := left for i from left to right − 1 do if list[i] <= pivotValue then swap list[storeIndex] and list[i] increment storeIndex swap list[right] and list[storeIndex] // Move pivot to its final place return storeIndex

This is known as the Lomuto partition scheme, which is simpler but less efficient than Hoare's original partition scheme. In quicksort, we recursively sort both branches, leading to best-case O ( n log ⁡ n ) {\displaystyle O(n\log n)} time. However, when doing selection, we already know which partition our desired element lies in, since the pivot is in its final sorted position, with all those preceding it in an unsorted order and all those following it in an unsorted order. Therefore, a single recursive call locates the desired element in the correct partition, and we build upon this for quickselect:

// Returns the k-th smallest element of list within left..right inclusive // (i.e. left <= k <= right). function select(list, left, right, k) is if left = right then // If the list contains only one element, return list[left] // return that element pivotIndex := ... // select a pivotIndex between left and right, // e.g., left + floor(rand() % (right − left + 1)) pivotIndex := partition(list, left, right, pivotIndex) // The pivot is in its final sorted position if k = pivotIndex then return list[k] else if k < pivotIndex then return select(list, left, pivotIndex − 1, k) else return select(list, pivotIndex + 1, right, k)

Just as the minimum-based selection algorithm is a partial selection sort, this is a partial quicksort, generating and partitioning only O ( log ⁡ n ) {\displaystyle O(\log n)} of its O ( n ) {\displaystyle O(n)} partitions. This simple procedure has expected linear performance, and, like quicksort, has quite good performance in practice. It is also an in-place algorithm, requiring only constant memory overhead if tail call optimization is available, or if eliminating the tail recursion with a loop:

function select(list, left, right, k) is loop if left = right then return list[left] pivotIndex := ... // select pivotIndex between left and right pivotIndex := partition(list, left, right, pivotIndex) if k = pivotIndex then return list[k] else if k < pivotIndex then right := pivotIndex − 1 else left := pivotIndex + 1

This select function works only with the Lomuto partition scheme. This is because select assumes that the return value from partition is the position of the pivot. This is true for the Lomuto partition scheme, but not for the Hoare's partition scheme. In Hoare's scheme, the return value is the last index of the "left" partition, and the pivot is not guaranteed to be "in between" the partitions. The recursion, as presented, also only works with Lomuto's scheme, as it assumes that the right partition starts with pivotIndex +1, while when using Hoare's scheme, the right partition starts at pivotIndex.

… excerpt ends here. Continue reading the full article.

Illustrations

Quickselect illustration

Worked examples

Example 1 — a first encounter with Quickselect

Start with the simplest possible case. Write down what Quickselect 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 Quickselect 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 Quickselect 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 Quickselect

In research
Quickselect 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 Quickselect 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
Quickselect is common in secondary-school and first-year university syllabi. It links to neighbouring topics Selection algorithms, Tony Hoare, so understanding it makes those chapters shorter.
In everyday life
Look for Quickselect 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.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Quickselect in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Quickselect 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 Quickselect out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Quickselect in simple terms?

In computer science, quickselect is a selection algorithm to find the kth smallest element in an unordered list, also known as the kth order statistic. Like the related quicksort sorting algorithm, it was developed by Tony Hoare, and thus is also known as Hoare's selection algorithm.

Why does Quickselect 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 Quickselect?

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 Quickselect.

Tags

  • Selection algorithms
  • Tony Hoare

Keep exploring