PLAIN/TEXT — ISSUE 225/456 COMPUTER SCIENCE · FOUNDATION / DIVIDE AND CONQUER · ~4 MIN READING DATE: 2027-05-14 ARCHIVE: https://plaintext.click/newsletter/ SUBJECT: How to find the middle number without sorting them all 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. / SOURCE TRAIL [1] MIT — Divide and conquer: median finding https://ocw.mit.edu/courses/6-046j-design-and-analysis-of-algorithms-spring-2015/resources/lecture-2-notes/ NEXT: Why a camera image flips and shrinks with distance --- HARD IDEAS. PLAIN TEXT. You are receiving this because you asked for one clear computer-science or physics idea each day. Unsubscribe: https://plaintext.click/unsubscribe View in browser: https://plaintext.click/newsletter/2027/05/2027-05-14-selection-without-sorting.html From: Plain/Text / Brickston Consulting · 30 N Gould St Ste N, Sheridan, WY 82801 US · knowledge@plaintext.click