How selection sort bounds the number of writes

Short answer: Selection sort scans the unsorted suffix for its minimum and swaps that minimum into the next output slot. The comparison count is always Θ(n²). The write count is Θ(n) swaps, one per output slot. Use it when each write is expensive and the array is short enough that the comparisons are acceptable.


What the problem actually asks

Some memories charge much more for a write than for a read: erasable storage, a write-limited device, or a record that is large to move and cheap to compare by a key. The sort should then move each record into its final slot as few times as possible. Finding the minimum of a suffix is enough to know that slot.

The input is still a comparison sort. Nothing about the write bound removes the need to look at pairs.


What the algorithm guarantees

There are exactly n−1 scans. Scan i inspects the suffix that starts at i, so the comparison count is n(n−1)/2 on every input, sorted or not. Each scan ends with one swap, and a swap of an item with itself can be skipped, so the number of real swaps is at most n−1. Extra memory is a few indices.

The standard swap is unstable. The minimum can leap over an earlier equal key that sits between i and the minimum’s old index. A stable variant shifts the gap closed instead of swapping. That variant writes Θ(n²) items in the worst case and gives up the write bound that justified the algorithm.


The working steps

  1. For i from 0 to n−2, treat the suffix a[i..] as unsorted.
  2. Scan that suffix and remember the index of its minimum.
  3. If that index is not i, swap a[i] with the minimum. Index i is now finished.
  4. Leave equal keys unswapped when the minimum’s key equals a[i], so a useless write is skipped.
  5. After i reaches n−2, the last item is in place.

The linear write bound counts swaps of records, under a comparison model that may read every pair. It is the wrong tool when the keys are integers in a small range. Counting sort then spends linear time and writes each record once into an output buffer.


Approaches that look simpler and fail

Approach Why it fails when the write budget is n swaps
Insertion sort Each inversion is one write. A reversed array performs Θ(n²) writes, which is the budget this sort exists to avoid.
A stable shift instead of a swap Closing the gap by sliding every intervening record writes those records. The write count returns to quadratic on a reversed array.
Stop early because a suffix looked ordered The comparison count is the full triangle of pairs. A sorted suffix is not detected, and skipping the scan can leave a smaller key behind.
Treat the Θ(n) swaps as a Θ(n) running time The scans still perform n(n−1)/2 comparisons. The linear term is only the writes.

A linear-time integer sort belongs to counting sort or radix sort, which also write each record a small constant number of times into an output array. A general comparison sort with an n log n bound belongs to heapsort.


A working example

The Bash script and the Python script selection-sort the same integers. Each pass writes the minimum of the unsorted prefix into place. Both print 1 2 2 3 4.

a=(4 2 2 3 1)
n=${#a[@]}
for ((i=0; i<n; i++)); do
  m=$i
  for ((j=i+1; j<n; j++)); do
    if (( a[j] < a[m] )); then m=$j; fi
  done
  t=${a[i]}; a[i]=${a[m]}; a[m]=$t
done
echo "${a[*]}"
a = [4, 2, 2, 3, 1]
for i in range(len(a)):
    m = min(range(i, len(a)), key=a.__getitem__)
    a[i], a[m] = a[m], a[i]
assert a == sorted(a)
print(*a)

Where it is used

Selection sort is not a library sort. Its bound on the number of swaps is why it still appears for a tiny list stored in memory that is expensive to write. Cycle sort writes even less. A long array uses introsort or timsort.


When you can drop this algorithm

Drop selection sort when comparisons dominate and n is large. Heapsort keeps the in-place constraint and replaces the quadratic scans with n log n. Drop the unstable swap when equal keys must keep their order, and pay for a stable algorithm instead. Keep selection sort when the array is short and each record may be written only once on its way to its final index.


Why this stays published

We publish this so a search for selection sort returns the n swaps and the n² comparisons as two separate bounds. Selection sort uses n swaps and always performs n(n−1)/2 comparisons, and the swap that makes it write-efficient is what makes the usual form unstable.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *