| ISSUE 225/456 · COMPUTER SCIENCE |
~4 MIN |
FOUNDATION / DIVIDE AND CONQUER · READING DATE 2027-05-14 · MAY 2027 — ALGORITHMS AND LIGHT
How to find the middle number without sorting them all
START HERE
You may need a typical response time without needing every recorded time in order. Selection is the computer task of finding the item at a specified position in sorted order. A selection procedure can discard irrelevant groups of values, avoiding the extra work of completely ordering every item.
|
/ WHY NOW
Finding the median, the middle-ranked value, is one example. Sorting the whole collection answers that question and many others you never asked. Selection instead uses comparisons to identify which part of the collection can still contain the required rank.
|
/ THE IDEA
Choose a value called a pivot. Partition the collection into values smaller than, equal to and greater than that pivot. Count the smaller group. That count tells you the positions occupied by the pivot group, even though neither outside group is sorted. If the desired rank is in the smaller group, continue there. If it lies beyond the pivot group, continue in the greater group with an adjusted rank. Otherwise the pivot value is the answer. The essential divide-and-conquer move is to recurse into only the relevant side, not to finish organising both sides.
THE FORMAL IDEA
right-side rank = k − L − E
| k = requested rank, counting the smallest as 1; L = number smaller than the pivot. | | E = number equal to the pivot. Use this adjustment only when k > L + E; the right group’s first position follows every smaller and equal value. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Values: [9, 1, 7, 3, 5]; ask for rank k = 3. Choose pivot 5: smaller [1,3], equal [5], greater [9,7]. L = 2 and E = 1, so rank 3 is the pivot: answer 5. Change only k to 4: search [9,7] for rank 4 − 2 − 1 = 1, giving 7.
|
The changed question does not require sorting the left group or putting 9 and 7 in complete order beforehand. Once the partition establishes their possible ranks, only the group containing the requested answer needs further attention.
/ SO WHAT?
Pivot choice controls how much work remains. Repeatedly choosing an extreme can leave almost the entire collection for the next step. Random pivots give expected linear work, while more careful deterministic pivot construction can guarantee linear worst-case work; those guarantees require more than this one lucky five-item partition.
ONE CAVEAT |
| Equal values must be handled explicitly, and many implementations rearrange their input while partitioning. Selection returns a ranked value, not a fully sorted output. A median convention for an even-sized collection may require selecting two central values and averaging them. |
KEEP THIS
Use a partition’s counts to keep only the group that can contain the requested rank, rather than sorting groups whose order cannot affect the answer.
|
NEXT: Why a camera image flips and shrinks with distance
|