THE SELECTION SORT pick the smallest, repeat — the classic unstable sort
Selection sort repeatedly finds the minimum of the unsorted remainder and swaps it into the next position. It is O(n²) like its cousins, but with a distinguishing trait: it makes the fewest swaps of any sort (exactly n−1), which matters when writes are expensive. And it is the textbook unstable sort — a long-range swap can leapfrog equal elements out of their original order — which makes it the perfect foil for the stability dart (511). Minimal writes, maximal comparisons, and ties not preserved.
THE TECHNIQUE select the min of the rest, swap it forward — n−1 swaps, UNSTABLE
The demo selection-sorts [5,2,4,1,3] by repeatedly swapping the minimum to the front: live demo
HISTORY & CREDIT selection sort · fewest swaps, unstable
“All simple sorts are basically the same.” — selection sort makes the fewest swaps and, unlike bubble/insertion, is unstable. cited
the select · find the minimum of the unsorted part; swap it into place. the swaps · exactly n−1 — the fewest of any sort (good when writes cost). the instability · a far swap can jump an element past an equal one — ties not preserved (dart 511).
The smallest pulled to the front again and again — fewest swaps, and ties rearranged. resource
RECOMMEND FOR I-13 the selected sort, on the compiler
On the canonical compiler, selection sort orders [5,2,4,1,3] correctly:
$ i13 run or_selectionsort.i13 # swap the min forward
RUN OK · 657 step(s) · peak stack 6 · call depth 6
is_sorted = 1
Recommend as a NULL — resource, and the foil for stability. Selection sort produces the same sorted order (for distinct keys) at O(n²) with minimal writes — a resource profile (B40). Its instability is not a keeper property but a lack of one; it exists here to set up dart 511. NULL.