{"id":111968,"date":"2026-01-06T07:00:00","date_gmt":"2026-01-06T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111968"},"modified":"2026-01-06T06:54:48","modified_gmt":"2026-01-06T14:54:48","slug":"20260106-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260106-00\/?p=111968","title":{"rendered":"Swapping two blocks of memory that reside inside a larger block, in constant memory, refinement"},"content":{"rendered":"<p>Commenter Neil Rashbrook <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260101-00\/?p=111955&amp;commentid=143684#comment-143684\"> pointed out<\/a> that my original rotation-based algorithm for swapping two blocks of memory inside a larger block did too much rotating. My solution had three rotations, but Neil was able to get it down to two.<\/p>\n<p>The set-up is that we have a large block of memory, and you want to swap two blocks that reside inside that large block. For concreteness, let&#8217;s say that it&#8217;s A1, A2, B1, B2, C1, C2, D1, D2, D3, E1, and you want to exchange the B&#8217;s with the D&#8217;s. Neil pointed out that you can start by rotating the BCD block to move the D&#8217;s to the front, producing ADBCE; and then rotate the BC block to move the C&#8217;s to the front, producing ADCBE.<\/p>\n<table style=\"border-collapse: collapse; text-align: center;\" title=\"See text\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">A1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">A2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background: lch(from currentcolor l c h \/ .1);\">B1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background: lch(from currentcolor l c h \/ .1);\">B2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">C1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">C2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, transparent, transparent .5em, lch(from currentcolor l c h \/ .2) .5em, lch(from currentcolor l c h \/ .2) 1em);\">D1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, lch(from currentcolor l c h \/ .2), lch(from currentcolor l c h \/ .2) .5em, transparent .5em, transparent 1em);\">D2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, transparent, transparent .5em, lch(from currentcolor l c h \/ .2) .5em, lch(from currentcolor l c h \/ .2) 1em);\">D3<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">E1<\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"10\"><!-- --><\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"2\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-top: none;\" colspan=\"4\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-top: none;\" colspan=\"3\"><!-- --><\/td>\n<td colspan=\"1\"><!-- --><\/td>\n<\/tr>\n<tr>\n<td colspan=\"3\">\u00a0<\/td>\n<td colspan=\"2\">\u2929<\/td>\n<td colspan=\"4\">\u00a0<\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"2\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-bottom: none;\" colspan=\"3\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-bottom: none;\" colspan=\"4\"><!-- --><\/td>\n<td colspan=\"1\"><!-- --><\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"10\"><!-- --><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">A1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">A2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, transparent, transparent .5em, lch(from currentcolor l c h \/ .2) .5em, lch(from currentcolor l c h \/ .2) 1em);\">D1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, lch(from currentcolor l c h \/ .2), lch(from currentcolor l c h \/ .2) .5em, transparent .5em, transparent 1em);\">D2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, transparent, transparent .5em, lch(from currentcolor l c h \/ .2) .5em, lch(from currentcolor l c h \/ .2) 1em);\">D3<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background: lch(from currentcolor l c h \/ .1);\">B1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background: lch(from currentcolor l c h \/ .1);\">B2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">C1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">C2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">E1<\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"10\"><!-- --><\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"2\"><!-- --><\/td>\n<td colspan=\"3\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-top: none;\" colspan=\"2\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-top: none;\" colspan=\"2\"><!-- --><\/td>\n<td colspan=\"1\"><!-- --><\/td>\n<\/tr>\n<tr>\n<td colspan=\"5\">\u00a0<\/td>\n<td colspan=\"4\">\u2929<\/td>\n<td colspan=\"1\">\u00a0<\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"2\"><!-- --><\/td>\n<td colspan=\"3\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-bottom: none;\" colspan=\"2\"><!-- --><\/td>\n<td style=\"border: solid 1px currentcolor; border-bottom: none;\" colspan=\"2\"><!-- --><\/td>\n<td colspan=\"1\"><!-- --><\/td>\n<\/tr>\n<tr style=\"height: 1ex;\">\n<td colspan=\"10\"><!-- --><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">A1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">A2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, transparent, transparent .5em, lch(from currentcolor l c h \/ .2) .5em, lch(from currentcolor l c h \/ .2) 1em);\">D1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, lch(from currentcolor l c h \/ .2), lch(from currentcolor l c h \/ .2) .5em, transparent .5em, transparent 1em);\">D2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background-image: repeating-linear-gradient(45deg, transparent, transparent .5em, lch(from currentcolor l c h \/ .2) .5em, lch(from currentcolor l c h \/ .2) 1em);\">D3<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">C1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">C2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background: lch(from currentcolor l c h \/ .1);\">B1<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor; background: lch(from currentcolor l c h \/ .1);\">B2<\/td>\n<td style=\"width: 2em; border: solid 1px currentcolor;\">E1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There is a symmetric version where you start by swapping the B&#8217;s to the end. If you choose to swap the larger block into position first, then the number of swaps is 2<var>n<\/var>\u00a0\u2212\u00a0max(|B|,|D|), which is a improvement over my three-rotation version that performed 2<var>n<\/var> swaps.<\/p>\n<p>(But still not as good as the <var>n<\/var> swaps that we developed later.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Could do with a little less rotating.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-111968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Could do with a little less rotating.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=111968"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111968\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=111968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}