{"id":252,"date":"2026-11-13T12:35:57","date_gmt":"2026-11-13T04:35:57","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-selection-sort-bounds-the-number-of-writes\/"},"modified":"2026-11-13T21:53:44","modified_gmt":"2026-11-13T13:53:44","slug":"how-selection-sort-bounds-the-number-of-writes","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-selection-sort-bounds-the-number-of-writes\/","title":{"rendered":"How selection sort bounds the number of writes"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Selection sort<\/strong> scans the unsorted suffix for its minimum and swaps that minimum into the next output slot. The comparison count is always <strong>\u0398(n\u00b2)<\/strong>. The write count is <strong>\u0398(n)<\/strong> swaps, one per output slot. Use it when each write is expensive and the array is short enough that the comparisons are acceptable.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The input is still a comparison sort. Nothing about the write bound removes the need to look at pairs.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">There are exactly n\u22121 scans. Scan i inspects the suffix that starts at i, so the comparison count is n(n\u22121)\/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\u22121. Extra memory is a few indices.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The standard swap is unstable. The minimum can leap over an earlier equal key that sits between i and the minimum&#8217;s old index. A stable variant shifts the gap closed instead of swapping. That variant writes \u0398(n\u00b2) items in the worst case and gives up the write bound that justified the algorithm.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>For i from 0 to n\u22122, treat the suffix a[i..] as unsorted.<\/li>\n<li>Scan that suffix and remember the index of its minimum.<\/li>\n<li>If that index is not i, swap a[i] with the minimum. Index i is now finished.<\/li>\n<li>Leave equal keys unswapped when the minimum&#8217;s key equals a[i], so a useless write is skipped.<\/li>\n<li>After i reaches n\u22122, the last item is in place.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the write budget is n swaps<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Insertion sort<\/td>\n<td>Each inversion is one write. A reversed array performs \u0398(n\u00b2) writes, which is the budget this sort exists to avoid.<\/td>\n<\/tr>\n<tr>\n<td>A stable shift instead of a swap<\/td>\n<td>Closing the gap by sliding every intervening record writes those records. The write count returns to quadratic on a reversed array.<\/td>\n<\/tr>\n<tr>\n<td>Stop early because a suffix looked ordered<\/td>\n<td>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.<\/td>\n<\/tr>\n<tr>\n<td>Treat the \u0398(n) swaps as a \u0398(n) running time<\/td>\n<td>The scans still perform n(n\u22121)\/2 comparisons. The linear term is only the writes.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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 <code>1 2 2 3 4<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">a=(4 2 2 3 1)\nn=${#a[@]}\nfor ((i=0; i&lt;n; i++)); do\n  m=$i\n  for ((j=i+1; j&lt;n; j++)); do\n    if (( a[j] &lt; a[m] )); then m=$j; fi\n  done\n  t=${a[i]}; a[i]=${a[m]}; a[m]=$t\ndone\necho &quot;${a[*]}&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">a = [4, 2, 2, 3, 1]\nfor i in range(len(a)):\n    m = min(range(i, len(a)), key=a.__getitem__)\n    a[i], a[m] = a[m], a[i]\nassert a == sorted(a)\nprint(*a)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for selection sort returns the n swaps and the n\u00b2 comparisons as two separate bounds. <strong>Selection sort uses n swaps and always performs n(n\u22121)\/2 comparisons, and the swap that makes it write-efficient is what makes the usual form unstable.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u0398(n\u00b2). The write count is \u0398(n) swaps, one per output slot. Use it when each write is expensive and the array is short enough&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,31],"tags":[],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sorting"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/252\/revisions\/651"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}