← Course home

Search and ordering · Lesson 24 of 52

Order-statistic selection

Find one sorted rank with quickselect without ordering every item.

Full lessonAbout 10 min

You will learn to trace quickselect's remaining side and compare expected and worst-case work.

Recommended first: Quicksort and partition.

On this page 6 sections

The idea

quickselect on 3, 1, 4, 2 with k = 2 returns 3.

An order statistic is the k-th smallest key, counting from zero. Sorting would find it, and would also order every other key. quickselect stops when the pivot lands on k. On a copy of 3, 1, 4, 2, quickselect with k = 0 returns 1. The same input with k = 2 returns 3, the third smallest key in 1, 2, 3, 4. The library partition always uses the rightmost value as the pivot. It walks the range once, swaps smaller keys toward the left boundary, and writes the pivot into the boundary. If that index is k, the pivot is the answer. If k is smaller, the search continues on the left side only. If k is larger, it continues on the right side only. The discarded side is not sorted.

A worked trace

StepStateWhat changed
Partitionindex = 1Pivot 2 lands at index 1. The array is [1, 2, 4, 3].
Partitionindex = 2Pivot 3 lands at index 2. The array is [1, 2, 3, 4].
Answerk = 2k is 2, so the value at that index is the answer 3.

Why it is correct

The partition invariant matches quicksort. Everything before the boundary is less than or equal to the pivot, and everything after it is greater. The pivot is therefore in its final sorted index even though the two sides are not sorted. The k-th smallest key must lie on the side that still contains index k. Throwing the other side away cannot hide the answer. The loop ends when the pivot index equals k, so the value sitting there is the order statistic.

A common mistake

The discarded side is not sorted, and it cannot contain the k-th smallest key.

What it costs

A balanced pivot throws away a constant fraction of the range, and the expected cost of that recurrence is linear. This implementation does not choose a random pivot. It always uses the right end. An input that always puts an extreme key at the right end shrinks the range by one item and the time becomes quadratic. Extra memory is a handful of indexes. Sort the whole array when you need every order statistic. Select when you need one. Expected time is Θ(n), and extra memory is O(1).

Run it

python
from bitsandbytes.sorting.selection_and_radix import quickselectprint(quickselect([3, 1, 4, 2], 0))# prints: 1 

Practice and next step

Open Inversion counting next. Inversion counting measures how far a list is from sorted while it merges.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.sorting.selection_and_radix

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Quickselect on a small array

quickselect uses the rightmost value as the pivot. k = 0 returns 1 and k = 2 returns 3 on 3, 1, 4, 2.

index
1

State

  1. 01chosen
  2. 12chosen
  3. 24chosen
  4. 33chosen

Pivot 2 lands at index 1. The array is [1, 2, 4, 3].

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1quickselect keeps searching after a partition. Which side remains?
2This quickselect always pivots on the rightmost value. What can the worst case cost?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.