Skip to the workspace
Pebellslearn by building
Sorting at Scale  ›  Lesson

Quickselect & Order Statistics

Selection & Order Statistics 24 minFind the kth smallest item by partitioning like quick sort, but recursing into only the one side that can hold it

dsa / Quickselect & Order Statistics

Explore each scene at your own pace.

01 · Find the median latency without sorting the log

Find the median latency without sorting the log

Quickselect & Order Statistics

Definition
Quickselect finds the kth smallest item in an unsorted list by partitioning around a pivot, as quick sort does, then continuing into only the one side that must contain the answer. On average this takes linear time, far less than a full sort.
In plain words
Suppose you want the 3rd shortest person in a crowd. Pick one person as a marker, send shorter people to one side and taller to the other, and see which side your target must be on. Then ignore the other side completely and repeat.

In short: finding the kth smallest item does not need a full sort. Quick sort's partition step (section 3) already tells you exactly where one item — the pivot — belongs in sorted order. If that spot is k, you're done. If it's past k, the answer is somewhere in the left part; if it's before k, the answer is in the right part. Either way, only one side ever gets partitioned again — the other is thrown away, unsorted, forever. That turns an O(n log n) sort into an O(n) average-case selection, because the work per round shrinks geometrically: roughly n + n/2 + n/4 + …, which sums to about 2n.

A row of six boxes split by a dashed pivot line: the four boxes holding values less than the pivot are green, and the two boxes past the line are crossed out with a red X, marked "never sorted, never revisited"

Find the median latency without sorting the log

Quarry's dashboard needs the median response time from a batch of requests, refreshed constantly. Sorting the whole log every refresh is wasted work — the median is just one or two positions, found with quickselect:

python

Watch quickselect hunt for the 3rd smallest of six values, discarding a side every round instead of sorting all six:

watch it move · array

Every round partitions around one pivot and keeps only the side that can still hold the target rank. The other side is never looked at again.

Finding the 3rd smallest (target index 2). Pivot on the last value, 15. 6 cells. At index 5.

1 / 7
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the ingest sorter
This piece — median_latency(): Reads the median request latency for a dashboard tile without sorting the whole log.
Scenario An operations dashboard shows the median request latency, refreshed every few seconds — too often to sort the whole log each time.
median_latency.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
median_latency(latencies) → floatThe median latency, found with quickselect instead of a full sort.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.