https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Heap%27s_algorithm Heap's algorithm - Revision history 2025-05-30T04:10:30Z Revision history for this page on the wiki MediaWiki 1.45.0-wmf.3 https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1267750292&oldid=prev MarSch: /* Details of the algorithm */ 2025-01-06T13:59:15Z <p><span class="autocomment">Details of the algorithm</span></p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 13:59, 6 January 2025</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 41:</td> <td colspan="2" class="diff-lineno">Line 41:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>procedure permutations(n : integer, A : array of any):</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>procedure permutations(n : integer, A : array of any):</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> // c is an encoding of the stack state. c[k] encodes the for-loop counter for when <del style="font-weight: bold; text-decoration: none;">generate</del>(k - 1, A) is called</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> // c is an encoding of the stack state.</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"> //</ins> c[k] encodes the for-loop counter for when <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k - 1, A) is called</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c : array of int</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c : array of int</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> </table> MarSch https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1267747015&oldid=prev MarSch: complete changing name of procedure to permutations and change comments of main algorithm 2025-01-06T13:33:49Z <p>complete changing name of procedure to permutations and change comments of main algorithm</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 13:33, 6 January 2025</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 14:</td> <td colspan="2" class="diff-lineno">Line 14:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// Output the k! permutations of A in which the first k elements are permuted in all ways.</div></td> </tr> <tr> <td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_3_4_rhs">&#x26AB;</a></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_2_0_lhs"></a>procedure <del style="font-weight: bold; text-decoration: none;">generate</del>(k : integer, A : array of any):</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// To get all permutations of A, use k := length of A.</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>//</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// If, k &gt; length of A, will try to access A out of bounds.</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// If k &lt;= 0 there will be no output (empty array has no permutations)</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_2_0_lhs">&#x26AB;</a></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_3_4_rhs"></a>procedure <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k : integer, A : array of any):</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> //<del style="font-weight: bold; text-decoration: none;"> Generate</del> permutations with <del style="font-weight: bold; text-decoration: none;">k-th</del> <del style="font-weight: bold; text-decoration: none;">unaltered</del></div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> // permutations with <ins style="font-weight: bold; text-decoration: none;">last element</ins> <ins style="font-weight: bold; text-decoration: none;">fixed</ins></div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">//</del> <del style="font-weight: bold; text-decoration: none;">Initially</del> <del style="font-weight: bold; text-decoration: none;">k</del> <del style="font-weight: bold; text-decoration: none;">= length(</del>A)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">permutations(k</ins> <ins style="font-weight: bold; text-decoration: none;">-</ins> <ins style="font-weight: bold; text-decoration: none;">1,</ins> A)</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_8_1_lhs">&#x26AB;</a></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_6_0_rhs"></a> // permutations <ins style="font-weight: bold; text-decoration: none;">with</ins> <ins style="font-weight: bold; text-decoration: none;">last element</ins> swapped <ins style="font-weight: bold; text-decoration: none;">out</ins></div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> generate(k - 1, A)</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><br /></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_6_0_rhs">&#x26AB;</a></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_8_1_lhs"></a> //<del style="font-weight: bold; text-decoration: none;"> Generate</del> permutations <del style="font-weight: bold; text-decoration: none;">for</del> <del style="font-weight: bold; text-decoration: none;">k-th</del> swapped <del style="font-weight: bold; text-decoration: none;">with each k-1 initial</del></div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k-1; i += 1 do</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k-1; i += 1 do</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> // Swap choice dependent on parity of k (even or odd)</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k is even then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k is even then</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> swap(A[i], A[k-1])<del style="font-weight: bold; text-decoration: none;"> // zero-indexed, the k-th is at k-1</del></div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> swap(A[i], A[k-1])</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> swap(A[0], A[k-1])</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> swap(A[0], A[k-1])</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end if</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end if</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">generate</del>(k - 1, A)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k - 1, A)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end for</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end for</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end if</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end if</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 38:</td> <td colspan="2" class="diff-lineno">Line 40:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>procedure <del style="font-weight: bold; text-decoration: none;">generate</del>(n : integer, A : array of any):</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>procedure <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(n : integer, A : array of any):</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // c is an encoding of the stack state. c[k] encodes the for-loop counter for when generate(k - 1, A) is called</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // c is an encoding of the stack state. c[k] encodes the for-loop counter for when generate(k - 1, A) is called</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c : array of int</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c : array of int</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 63:</td> <td colspan="2" class="diff-lineno">Line 65:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i := 1</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i := 1</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> // Calling <del style="font-weight: bold; text-decoration: none;">generate</del>(i+1, A) has ended as the while-loop terminated. Reset the state and simulate popping the stack by incrementing the pointer.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> // Calling <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(i+1, A) has ended as the while-loop terminated. Reset the state and simulate popping the stack by incrementing the pointer.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c[i] := 0</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c[i] := 0</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i += 1</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i += 1</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 141:</td> <td colspan="2" class="diff-lineno">Line 143:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>procedure <del style="font-weight: bold; text-decoration: none;">generate</del>(k : integer, A : array of any):</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>procedure <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k : integer, A : array of any):</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 148:</td> <td colspan="2" class="diff-lineno">Line 150:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // Recursively call once for each k</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // Recursively call once for each k</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k; i += 1 do</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k; i += 1 do</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">generate</del>(k - 1, A)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k - 1, A)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // swap choice dependent on parity of k (even or odd)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // swap choice dependent on parity of k (even or odd)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k is even then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k is even then</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 193:</td> <td colspan="2" class="diff-lineno">Line 195:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>procedure <del style="font-weight: bold; text-decoration: none;">generate</del>(k : integer, A : array of any):</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>procedure <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k : integer, A : array of any):</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 200:</td> <td colspan="2" class="diff-lineno">Line 202:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // Recursively call once for each k</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // Recursively call once for each k</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k; i += 1 do</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k; i += 1 do</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">generate</del>(k - 1, A)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k - 1, A)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // avoid swap when i==k-1</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // avoid swap when i==k-1</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if (i &lt; k - 1)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if (i &lt; k - 1)</div></td> </tr> </table> MarSch https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1267743027&oldid=prev MarSch: /* Proof */ make second case of induction more clear 2025-01-06T13:03:38Z <p><span class="autocomment">Proof: </span> make second case of induction more clear</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 13:03, 6 January 2025</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 96:</td> <td colspan="2" class="diff-lineno">Line 96:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array A has length {{mvar|n}}, then &lt;code&gt;permutations(n, A)&lt;/code&gt; will result in either A being unchanged, if {{mvar|n}} is odd, or, if {{mvar|n}} is even, then A is rotated to the right by 1 (last element shifted in front of other elements).</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array A has length {{mvar|n}}, then &lt;code&gt;permutations(n, A)&lt;/code&gt; will result in either A being unchanged, if {{mvar|n}} is odd, or, if {{mvar|n}} is even, then A is rotated to the right by 1 (last element shifted in front of other elements).</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">Basis</del>: If array {{mvar|A}} has length 1, then &lt;code&gt;permutations(1, A)&lt;/code&gt; will output A and stop, so A is unchanged. Since 1 is odd, this is what was claimed, so the claim is true for arrays of length 1.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;">Base</ins>: If array {{mvar|A}} has length 1, then &lt;code&gt;permutations(1, A)&lt;/code&gt; will output A and stop, so A is unchanged. Since 1 is odd, this is what was claimed, so the claim is true for arrays of length 1.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Induction: If the claim is true for arrays of length {{math|l}} ≥ 1, then we show that the claim is true for arrays of length {{math|l}}+1 (together with the base case this proves that the claim is true for arrays of all lengths). Since the claim depends on whether {{math|l}} is odd or even, we prove each case separately.</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Induction: If the claim is true for arrays of length {{math|l}} ≥ 1, then we show that the claim is true for arrays of length {{math|l}}+1 (together with the base case this proves that the claim is true for arrays of all lengths). Since the claim depends on whether {{math|l}} is odd or even, we prove each case separately.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>If {{math|l}} is odd, then, by the induction hypothesis, for an array A of length {{math|l}}, &lt;code&gt;permutations(l, A)&lt;/code&gt; will not change A, and for the claim to hold for arrays of length {{math|l}}+1, we need to show that &lt;code&gt;permutations(l+1, A)&lt;/code&gt; rotates A to the right by 1. Doing &lt;code&gt;permutations(l+1, A)&lt;/code&gt; will first do &lt;code&gt;permutations(l, A)&lt;/code&gt; (leaving A unchanged since {{math|l}} is odd) and then in each iteration {{math|i}} of the for-loop, swap the elements in positions {{math|i}} and {{math|l}} (the last position) in A. The first swap puts element {{math|l}} (the last element) in position 0, and element 0 in position {{math|l}}. The next swap puts the element in position {{math|l}} (where the previous iteration put original element 0) in position 1 and element 1 in position {{math|l}}. In the final iteration, the swap puts element {{math|l}}-1 is in position {{math|l}}, and the element in position {{math|l}} (where the previous iteration put original element {{math|l}}-2) in position {{math|l}}-1. To illustrate the above, look below for the case <del style="font-weight: bold; text-decoration: none;">&lt;</del>math<del style="font-weight: bold; text-decoration: none;">&gt;</del>n = 4<del style="font-weight: bold; text-decoration: none;">&lt;/math&gt;</del></div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If {{math|l}} is odd, then, by the induction hypothesis, for an array A of length {{math|l}}, &lt;code&gt;permutations(l, A)&lt;/code&gt; will not change A, and for the claim to hold for arrays of length {{math|l}}+1<ins style="font-weight: bold; text-decoration: none;"> (which is even)</ins>, we need to show that &lt;code&gt;permutations(l+1, A)&lt;/code&gt; rotates A to the right by 1<ins style="font-weight: bold; text-decoration: none;"> position</ins>. Doing &lt;code&gt;permutations(l+1, A)&lt;/code&gt; will first do &lt;code&gt;permutations(l, A)&lt;/code&gt; (leaving A unchanged since {{math|l}} is odd) and then in each iteration {{math|i}} of the for-loop, swap the elements in positions {{math|i}} and {{math|l}} (the last position) in A. The first swap puts element {{math|l}} (the last element) in position 0, and element 0 in position {{math|l}}. The next swap puts the element in position {{math|l}} (where the previous iteration put original element 0) in position 1 and element 1 in position {{math|l}}. In the final iteration, the swap puts element {{math|l}}-1 is in position {{math|l}}, and the element in position {{math|l}} (where the previous iteration put original element {{math|l}}-2) in position {{math|l}}-1. To illustrate the above, look below for the case <ins style="font-weight: bold; text-decoration: none;">{{</ins>math<ins style="font-weight: bold; text-decoration: none;">|</ins>n<ins style="font-weight: bold; text-decoration: none;">}}</ins> = 4<ins style="font-weight: bold; text-decoration: none;">.</ins></div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;pre&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;pre&gt;</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4 ... <del style="font-weight: bold; text-decoration: none;">Original</del> <del style="font-weight: bold; text-decoration: none;">Array</del></div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4 ... <ins style="font-weight: bold; text-decoration: none;">original</ins> <ins style="font-weight: bold; text-decoration: none;">array</ins></div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4 ... 1st iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4 ... 1st iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,2,3,1 ... 1st iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del> 1st element into <del style="font-weight: bold; text-decoration: none;">"buffer"</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,2,3,1 ... 1st iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins> 1st element into <ins style="font-weight: bold; text-decoration: none;">last position</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,2,3,1 ... 2nd iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,2,3,1 ... 2nd iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,1,3,2 ... 2nd iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del> 2nd element into <del style="font-weight: bold; text-decoration: none;">"buffer"</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,1,3,2 ... 2nd iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins> 2nd element into <ins style="font-weight: bold; text-decoration: none;">last position</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,1,3,2 ... 3rd iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,1,3,2 ... 3rd iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3 ... 3rd iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del> 3rd element into <del style="font-weight: bold; text-decoration: none;">"buffer"</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3 ... 3rd iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins> 3rd element into <ins style="font-weight: bold; text-decoration: none;">last position</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3 ... 4th iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3 ... 4th iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3 ... 4th iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del> 4th element into <del style="font-weight: bold; text-decoration: none;">"buffer"</del>)<del style="font-weight: bold; text-decoration: none;"> ... </del>The altered array is a rotated version of the original</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3 ... 4th iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins> 4th element into <ins style="font-weight: bold; text-decoration: none;">last position</ins>)</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>The altered array is a rotated version of the original</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/pre&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/pre&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>If, for {{<del style="font-weight: bold; text-decoration: none;">mvar</del>|<del style="font-weight: bold; text-decoration: none;">A</del>}}, &lt;<del style="font-weight: bold; text-decoration: none;">math</del>&gt;<del style="font-weight: bold; text-decoration: none;">n=i+1</del>&lt;/<del style="font-weight: bold; text-decoration: none;">math</del>&gt; <del style="font-weight: bold; text-decoration: none;">is</del> <del style="font-weight: bold; text-decoration: none;">odd,</del> <del style="font-weight: bold; text-decoration: none;">then</del> the <del style="font-weight: bold; text-decoration: none;">subset</del> <del style="font-weight: bold; text-decoration: none;">of</del> the <del style="font-weight: bold; text-decoration: none;">first</del> {{<del style="font-weight: bold; text-decoration: none;">mvar</del>|<del style="font-weight: bold; text-decoration: none;">i</del>}} <del style="font-weight: bold; text-decoration: none;">elements</del> <del style="font-weight: bold; text-decoration: none;">will</del> <del style="font-weight: bold; text-decoration: none;">be</del> <del style="font-weight: bold; text-decoration: none;">rotated</del> <del style="font-weight: bold; text-decoration: none;">after</del> <del style="font-weight: bold; text-decoration: none;">performing</del> <del style="font-weight: bold; text-decoration: none;">Heap's</del> <del style="font-weight: bold; text-decoration: none;">Algorithm</del> <del style="font-weight: bold; text-decoration: none;">on</del> <del style="font-weight: bold; text-decoration: none;">the</del> <del style="font-weight: bold; text-decoration: none;">first</del> <del style="font-weight: bold; text-decoration: none;">{{mvar|i}}</del> <del style="font-weight: bold; text-decoration: none;">elements</del>. <del style="font-weight: bold; text-decoration: none;">Notice</del> <del style="font-weight: bold; text-decoration: none;">that</del>, <del style="font-weight: bold; text-decoration: none;">after</del> <del style="font-weight: bold; text-decoration: none;">1</del> iteration of the for-loop, <del style="font-weight: bold; text-decoration: none;">when</del> <del style="font-weight: bold; text-decoration: none;">performing</del> <del style="font-weight: bold; text-decoration: none;">Heap's</del> <del style="font-weight: bold; text-decoration: none;">Algorithm</del> <del style="font-weight: bold; text-decoration: none;">on</del> {{<del style="font-weight: bold; text-decoration: none;">mvar</del>|<del style="font-weight: bold; text-decoration: none;">A</del>}}<del style="font-weight: bold; text-decoration: none;">,</del> {{<del style="font-weight: bold; text-decoration: none;">mvar</del>|<del style="font-weight: bold; text-decoration: none;">A</del>}} is <del style="font-weight: bold; text-decoration: none;">rotated</del> <del style="font-weight: bold; text-decoration: none;">to</del> the <del style="font-weight: bold; text-decoration: none;">right</del> <del style="font-weight: bold; text-decoration: none;">by</del> <del style="font-weight: bold; text-decoration: none;">1.</del> <del style="font-weight: bold; text-decoration: none;">By</del> <del style="font-weight: bold; text-decoration: none;">the</del> <del style="font-weight: bold; text-decoration: none;">induction</del> <del style="font-weight: bold; text-decoration: none;">hypothesis,</del> <del style="font-weight: bold; text-decoration: none;">it</del> <del style="font-weight: bold; text-decoration: none;">is</del> <del style="font-weight: bold; text-decoration: none;">assumed</del> <del style="font-weight: bold; text-decoration: none;">that</del> the first {{<del style="font-weight: bold; text-decoration: none;">mvar</del>|<del style="font-weight: bold; text-decoration: none;">i</del>}} elements <del style="font-weight: bold; text-decoration: none;">will</del> <del style="font-weight: bold; text-decoration: none;">rotate.</del> <del style="font-weight: bold; text-decoration: none;">After this rotation,</del> the first <del style="font-weight: bold; text-decoration: none;">element</del> <del style="font-weight: bold; text-decoration: none;">of</del> <del style="font-weight: bold; text-decoration: none;">{{mvar|A}}</del> <del style="font-weight: bold; text-decoration: none;">will</del> <del style="font-weight: bold; text-decoration: none;">be</del> <del style="font-weight: bold; text-decoration: none;">swapped</del> <del style="font-weight: bold; text-decoration: none;">into</del> the <del style="font-weight: bold; text-decoration: none;">buffer</del> <del style="font-weight: bold; text-decoration: none;">which,</del> <del style="font-weight: bold; text-decoration: none;">when</del> <del style="font-weight: bold; text-decoration: none;">combined</del> <del style="font-weight: bold; text-decoration: none;">with</del> <del style="font-weight: bold; text-decoration: none;">the</del> <del style="font-weight: bold; text-decoration: none;">previous</del> <del style="font-weight: bold; text-decoration: none;">rotation</del> <del style="font-weight: bold; text-decoration: none;">operation,</del> <del style="font-weight: bold; text-decoration: none;">will</del> <del style="font-weight: bold; text-decoration: none;">in</del> <del style="font-weight: bold; text-decoration: none;">essence</del> <del style="font-weight: bold; text-decoration: none;">perform</del> <del style="font-weight: bold; text-decoration: none;">a</del> <del style="font-weight: bold; text-decoration: none;">rotation</del> <del style="font-weight: bold; text-decoration: none;">on</del> the array<del style="font-weight: bold; text-decoration: none;">.</del> <del style="font-weight: bold; text-decoration: none;">Perform</del> <del style="font-weight: bold; text-decoration: none;">this</del> <del style="font-weight: bold; text-decoration: none;">rotation</del> <del style="font-weight: bold; text-decoration: none;">operation</del> <del style="font-weight: bold; text-decoration: none;">{{mvar|n}}</del> <del style="font-weight: bold; text-decoration: none;">times,</del> <del style="font-weight: bold; text-decoration: none;">and</del> <del style="font-weight: bold; text-decoration: none;">the</del> <del style="font-weight: bold; text-decoration: none;">array will revert</del> to <del style="font-weight: bold; text-decoration: none;">its</del> <del style="font-weight: bold; text-decoration: none;">original</del> <del style="font-weight: bold; text-decoration: none;">state</del>. <del style="font-weight: bold; text-decoration: none;">This</del> <del style="font-weight: bold; text-decoration: none;">is</del> <del style="font-weight: bold; text-decoration: none;">illustrated</del> below for the case <del style="font-weight: bold; text-decoration: none;">&lt;</del>math<del style="font-weight: bold; text-decoration: none;">&gt;</del>n = 5<del style="font-weight: bold; text-decoration: none;">&lt;/math&gt;</del>.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If<ins style="font-weight: bold; text-decoration: none;"> {{math|l}} is even, then, by the induction hypothesis</ins>, for<ins style="font-weight: bold; text-decoration: none;"> an array A of length</ins> {{<ins style="font-weight: bold; text-decoration: none;">math</ins>|<ins style="font-weight: bold; text-decoration: none;">l</ins>}}, &lt;<ins style="font-weight: bold; text-decoration: none;">code</ins>&gt;<ins style="font-weight: bold; text-decoration: none;">permutations(l, A)</ins>&lt;/<ins style="font-weight: bold; text-decoration: none;">code</ins>&gt; <ins style="font-weight: bold; text-decoration: none;">rotates</ins> <ins style="font-weight: bold; text-decoration: none;">A</ins> <ins style="font-weight: bold; text-decoration: none;">to</ins> the <ins style="font-weight: bold; text-decoration: none;">right</ins> <ins style="font-weight: bold; text-decoration: none;">by 1 position, and for</ins> the <ins style="font-weight: bold; text-decoration: none;">claim to hold for arrays of length</ins> {{<ins style="font-weight: bold; text-decoration: none;">math</ins>|<ins style="font-weight: bold; text-decoration: none;">l</ins>}}<ins style="font-weight: bold; text-decoration: none;">+1</ins> <ins style="font-weight: bold; text-decoration: none;">(which</ins> <ins style="font-weight: bold; text-decoration: none;">is</ins> <ins style="font-weight: bold; text-decoration: none;">odd),</ins> <ins style="font-weight: bold; text-decoration: none;">we</ins> <ins style="font-weight: bold; text-decoration: none;">need</ins> <ins style="font-weight: bold; text-decoration: none;">to</ins> <ins style="font-weight: bold; text-decoration: none;">show</ins> <ins style="font-weight: bold; text-decoration: none;">that</ins> <ins style="font-weight: bold; text-decoration: none;">&lt;code&gt;permutations(l+1,</ins> <ins style="font-weight: bold; text-decoration: none;">A)&lt;/code&gt;</ins> <ins style="font-weight: bold; text-decoration: none;">leaves</ins> <ins style="font-weight: bold; text-decoration: none;">A</ins> <ins style="font-weight: bold; text-decoration: none;">unchanged</ins>. <ins style="font-weight: bold; text-decoration: none;">Doing</ins> <ins style="font-weight: bold; text-decoration: none;">&lt;code&gt;permutations(l+1</ins>, <ins style="font-weight: bold; text-decoration: none;">A)&lt;/code&gt;</ins> <ins style="font-weight: bold; text-decoration: none;">will in each</ins> iteration<ins style="font-weight: bold; text-decoration: none;"> {{math|i}}</ins> of the for-loop, <ins style="font-weight: bold; text-decoration: none;">first</ins> <ins style="font-weight: bold; text-decoration: none;">do</ins> <ins style="font-weight: bold; text-decoration: none;">&lt;code&gt;permutations(l,</ins> <ins style="font-weight: bold; text-decoration: none;">A)&lt;/code&gt;</ins> <ins style="font-weight: bold; text-decoration: none;">(rotating the first</ins> {{<ins style="font-weight: bold; text-decoration: none;">math</ins>|<ins style="font-weight: bold; text-decoration: none;">l</ins>}}<ins style="font-weight: bold; text-decoration: none;"> elements of A by 1 position since</ins> {{<ins style="font-weight: bold; text-decoration: none;">math</ins>|<ins style="font-weight: bold; text-decoration: none;">l</ins>}} is <ins style="font-weight: bold; text-decoration: none;">even)</ins> <ins style="font-weight: bold; text-decoration: none;">and then, swap</ins> the <ins style="font-weight: bold; text-decoration: none;">elements</ins> <ins style="font-weight: bold; text-decoration: none;">in</ins> <ins style="font-weight: bold; text-decoration: none;">positions</ins> <ins style="font-weight: bold; text-decoration: none;">0</ins> <ins style="font-weight: bold; text-decoration: none;">and</ins> <ins style="font-weight: bold; text-decoration: none;">{{math|l}}</ins> <ins style="font-weight: bold; text-decoration: none;">(the</ins> <ins style="font-weight: bold; text-decoration: none;">last</ins> <ins style="font-weight: bold; text-decoration: none;">position)</ins> <ins style="font-weight: bold; text-decoration: none;">in</ins> <ins style="font-weight: bold; text-decoration: none;">A. Rotating</ins> the first {{<ins style="font-weight: bold; text-decoration: none;">math</ins>|<ins style="font-weight: bold; text-decoration: none;">l</ins>}} elements <ins style="font-weight: bold; text-decoration: none;">and</ins> <ins style="font-weight: bold; text-decoration: none;">then</ins> <ins style="font-weight: bold; text-decoration: none;">swapping</ins> the first <ins style="font-weight: bold; text-decoration: none;">and</ins> <ins style="font-weight: bold; text-decoration: none;">last</ins> <ins style="font-weight: bold; text-decoration: none;">elements</ins> <ins style="font-weight: bold; text-decoration: none;">is</ins> <ins style="font-weight: bold; text-decoration: none;">equivalent</ins> <ins style="font-weight: bold; text-decoration: none;">to</ins> <ins style="font-weight: bold; text-decoration: none;">rotating</ins> the <ins style="font-weight: bold; text-decoration: none;">entire</ins> <ins style="font-weight: bold; text-decoration: none;">array.</ins> <ins style="font-weight: bold; text-decoration: none;">Since</ins> <ins style="font-weight: bold; text-decoration: none;">there</ins> <ins style="font-weight: bold; text-decoration: none;">are</ins> <ins style="font-weight: bold; text-decoration: none;">as</ins> <ins style="font-weight: bold; text-decoration: none;">many</ins> <ins style="font-weight: bold; text-decoration: none;">iterations</ins> <ins style="font-weight: bold; text-decoration: none;">of</ins> <ins style="font-weight: bold; text-decoration: none;">the</ins> <ins style="font-weight: bold; text-decoration: none;">loop</ins> <ins style="font-weight: bold; text-decoration: none;">as</ins> <ins style="font-weight: bold; text-decoration: none;">there</ins> <ins style="font-weight: bold; text-decoration: none;">are</ins> <ins style="font-weight: bold; text-decoration: none;">elements</ins> <ins style="font-weight: bold; text-decoration: none;">in</ins> the array<ins style="font-weight: bold; text-decoration: none;">,</ins> <ins style="font-weight: bold; text-decoration: none;">the</ins> <ins style="font-weight: bold; text-decoration: none;">entire</ins> <ins style="font-weight: bold; text-decoration: none;">array</ins> <ins style="font-weight: bold; text-decoration: none;">is</ins> <ins style="font-weight: bold; text-decoration: none;">rotated</ins> <ins style="font-weight: bold; text-decoration: none;">until</ins> <ins style="font-weight: bold; text-decoration: none;">each</ins> <ins style="font-weight: bold; text-decoration: none;">element</ins> <ins style="font-weight: bold; text-decoration: none;">returns</ins> to <ins style="font-weight: bold; text-decoration: none;">where</ins> <ins style="font-weight: bold; text-decoration: none;">it</ins> <ins style="font-weight: bold; text-decoration: none;">started</ins>. <ins style="font-weight: bold; text-decoration: none;">To</ins> <ins style="font-weight: bold; text-decoration: none;">illustrate</ins> <ins style="font-weight: bold; text-decoration: none;">the above, look</ins> below for the case <ins style="font-weight: bold; text-decoration: none;">{{</ins>math<ins style="font-weight: bold; text-decoration: none;">|</ins>n<ins style="font-weight: bold; text-decoration: none;">}}</ins> = 5.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;pre&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;pre&gt;</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4,5 ... <del style="font-weight: bold; text-decoration: none;">Original</del> <del style="font-weight: bold; text-decoration: none;">Array</del></div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4,5 ... <ins style="font-weight: bold; text-decoration: none;">original</ins> <ins style="font-weight: bold; text-decoration: none;">array</ins></div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3,5 ... 1st iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset/Rotate</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,1,2,3,5 ... 1st iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray,</ins> <ins style="font-weight: bold; text-decoration: none;">which rotates it</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>5,1,2,3,4 ... 1st iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>5,1,2,3,4 ... 1st iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>3,5,1,2,4 ... 2nd iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset/Rotate</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>3,5,1,2,4 ... 2nd iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray,</ins> <ins style="font-weight: bold; text-decoration: none;">which rotates it</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>4,5,1,2,3 ... 2nd iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>4,5,1,2,3 ... 2nd iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>2,4,5,1,3 ... 3rd iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset/Rotate</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>2,4,5,1,3 ... 3rd iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray,</ins> <ins style="font-weight: bold; text-decoration: none;">which rotates it</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>3,4,5,1,2 ... 3rd iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>3,4,5,1,2 ... 3rd iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>1,3,4,5,2 ... 4th iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset/Rotate</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>1,3,4,5,2 ... 4th iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray,</ins> <ins style="font-weight: bold; text-decoration: none;">which rotates it</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>2,3,4,5,1 ... 4th iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>2,3,4,5,1 ... 4th iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>5,2,3,4,1 ... 5th iteration (<del style="font-weight: bold; text-decoration: none;">Permute</del> <del style="font-weight: bold; text-decoration: none;">subset/Rotate</del> <del style="font-weight: bold; text-decoration: none;">subset</del>)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>5,2,3,4,1 ... 5th iteration (<ins style="font-weight: bold; text-decoration: none;">permute</ins> <ins style="font-weight: bold; text-decoration: none;">subarray,</ins> <ins style="font-weight: bold; text-decoration: none;">which rotates it</ins>)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4,5 ... 5th iteration (<del style="font-weight: bold; text-decoration: none;">Swap</del>)<del style="font-weight: bold; text-decoration: none;"> ... </del>The final state of the array is in the same order as the original</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4,5 ... 5th iteration (<ins style="font-weight: bold; text-decoration: none;">swap</ins>)</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>The final state of the array is in the same order as the original</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/pre&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/pre&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> </table> MarSch https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1267731245&oldid=prev MarSch: /* Proof */ make first case of induction more clear 2025-01-06T11:21:56Z <p><span class="autocomment">Proof: </span> make first case of induction more clear</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 11:21, 6 January 2025</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 94:</td> <td colspan="2" class="diff-lineno">Line 94:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/syntaxhighlight&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/syntaxhighlight&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array <del style="font-weight: bold; text-decoration: none;">{{mvar|</del>A<del style="font-weight: bold; text-decoration: none;">}}</del> has length {{mvar|n}}, then &lt;code&gt;permutations(n, A)&lt;/code&gt; will result in <del style="font-weight: bold; text-decoration: none;">one</del> <del style="font-weight: bold; text-decoration: none;">of</del> <del style="font-weight: bold; text-decoration: none;">two</del> <del style="font-weight: bold; text-decoration: none;">possibilities.</del> <del style="font-weight: bold; text-decoration: none;">If</del> {{mvar|n}} is odd, <del style="font-weight: bold; text-decoration: none;">then {{mvar|A}} is unaltered.</del> <del style="font-weight: bold; text-decoration: none;">If</del> {{mvar|n}} is even, then <del style="font-weight: bold; text-decoration: none;">{{mvar|</del>A<del style="font-weight: bold; text-decoration: none;">}}</del> is <del style="font-weight: bold; text-decoration: none;">"</del>rotated<del style="font-weight: bold; text-decoration: none;">"</del> to the right by 1 (last element shifted in front of other elements).</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array A has length {{mvar|n}}, then &lt;code&gt;permutations(n, A)&lt;/code&gt; will result in <ins style="font-weight: bold; text-decoration: none;">either</ins> <ins style="font-weight: bold; text-decoration: none;">A</ins> <ins style="font-weight: bold; text-decoration: none;">being</ins> <ins style="font-weight: bold; text-decoration: none;">unchanged,</ins> <ins style="font-weight: bold; text-decoration: none;">if</ins> {{mvar|n}} is odd, <ins style="font-weight: bold; text-decoration: none;">or,</ins> <ins style="font-weight: bold; text-decoration: none;">if</ins> {{mvar|n}} is even, then A is rotated to the right by 1 (last element shifted in front of other elements).</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Basis: If array {{mvar|A}} has length 1, then &lt;code&gt;permutations(1, A)&lt;/code&gt; will output A and stop, so A is <del style="font-weight: bold; text-decoration: none;">unaltered</del>. Since 1 is odd, this is what was claimed, so the claim <del style="font-weight: bold; text-decoration: none;">holds</del>.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Basis: If array {{mvar|A}} has length 1, then &lt;code&gt;permutations(1, A)&lt;/code&gt; will output A and stop, so A is <ins style="font-weight: bold; text-decoration: none;">unchanged</ins>. Since 1 is odd, this is what was claimed, so the claim <ins style="font-weight: bold; text-decoration: none;">is true for arrays of length 1</ins>.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Induction: <del style="font-weight: bold; text-decoration: none;">Assume</del> the claim <del style="font-weight: bold; text-decoration: none;">holds</del> true for <del style="font-weight: bold; text-decoration: none;">some</del> <del style="font-weight: bold; text-decoration: none;">&lt;math&gt;i</del> <del style="font-weight: bold; text-decoration: none;">\geq</del> <del style="font-weight: bold; text-decoration: none;">1&lt;/</del>math<del style="font-weight: bold; text-decoration: none;">&gt;.</del> <del style="font-weight: bold; text-decoration: none;">We</del> <del style="font-weight: bold; text-decoration: none;">will</del> then <del style="font-weight: bold; text-decoration: none;">need</del> <del style="font-weight: bold; text-decoration: none;">to</del> <del style="font-weight: bold; text-decoration: none;">handle</del> <del style="font-weight: bold; text-decoration: none;">two</del> <del style="font-weight: bold; text-decoration: none;">cases</del> for <del style="font-weight: bold; text-decoration: none;">&lt;</del>math<del style="font-weight: bold; text-decoration: none;">&gt;i</del>+1<del style="font-weight: bold; text-decoration: none;">&lt;/math&gt;:</del> <del style="font-weight: bold; text-decoration: none;">&lt;</del>math<del style="font-weight: bold; text-decoration: none;">&gt;i+1&lt;/math&gt;</del> is <del style="font-weight: bold; text-decoration: none;">even</del> or <del style="font-weight: bold; text-decoration: none;">odd</del>.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Induction: <ins style="font-weight: bold; text-decoration: none;">If</ins> the claim <ins style="font-weight: bold; text-decoration: none;">is</ins> true for <ins style="font-weight: bold; text-decoration: none;">arrays</ins> <ins style="font-weight: bold; text-decoration: none;">of</ins> <ins style="font-weight: bold; text-decoration: none;">length</ins> <ins style="font-weight: bold; text-decoration: none;">{{</ins>math<ins style="font-weight: bold; text-decoration: none;">|l}}</ins> <ins style="font-weight: bold; text-decoration: none;">≥</ins> <ins style="font-weight: bold; text-decoration: none;">1,</ins> then <ins style="font-weight: bold; text-decoration: none;">we</ins> <ins style="font-weight: bold; text-decoration: none;">show</ins> <ins style="font-weight: bold; text-decoration: none;">that</ins> <ins style="font-weight: bold; text-decoration: none;">the</ins> <ins style="font-weight: bold; text-decoration: none;">claim is true</ins> for <ins style="font-weight: bold; text-decoration: none;">arrays of length {{</ins>math<ins style="font-weight: bold; text-decoration: none;">|l}}</ins>+1 <ins style="font-weight: bold; text-decoration: none;">(together with the base case this proves that the claim is true for arrays of all lengths). Since the claim depends on whether {{</ins>math<ins style="font-weight: bold; text-decoration: none;">|l}}</ins> is <ins style="font-weight: bold; text-decoration: none;">odd</ins> or <ins style="font-weight: bold; text-decoration: none;">even, we prove each case separately</ins>.</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>If {{math|l}} is odd, then, by the induction hypothesis, for an array A of length {{math|l}}, &lt;code&gt;permutations(l, A)&lt;/code&gt; will not change A, and for the claim to hold for arrays of length {{math|l}}+1, we need to show that &lt;code&gt;permutations(l+1, A)&lt;/code&gt; rotates A to the right by 1. Doing &lt;code&gt;permutations(l+1, A)&lt;/code&gt; will first do &lt;code&gt;permutations(l, A)&lt;/code&gt; (leaving A unchanged since {{math|l}} is odd) and then in each iteration {{math|i}} of the for-loop, swap the elements in positions {{math|i}} and {{math|l}} (the last position) in A. The first swap puts element {{math|l}} (the last element) in position 0, and element 0 in position {{math|l}}. The next swap puts the element in position {{math|l}} (where the previous iteration put original element 0) in position 1 and element 1 in position {{math|l}}. In the final iteration, the swap puts element {{math|l}}-1 is in position {{math|l}}, and the element in position {{math|l}} (where the previous iteration put original element {{math|l}}-2) in position {{math|l}}-1. To illustrate the above, look below for the case &lt;math&gt;n = 4&lt;/math&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>If, for {{mvar|A}}, &lt;math&gt;n=i+1&lt;/math&gt; is even, then the subset of the first {{mvar|i}} elements will remain unaltered after performing Heap's Algorithm on the subarray, as assumed by the induction hypothesis. By performing Heap's Algorithm on the subarray and then performing the swapping operation, in the {{mvar|k}}th iteration of the for-loop, where &lt;math&gt;k \leq i+1&lt;/math&gt;, the {{mvar|k}}th element in {{mvar|A}} will be swapped into the last position of {{mvar|A}} which can be thought as a kind of "buffer". By swapping the 1st and last element, then swapping 2nd and last, all the way until the {{mvar|n}}th and last elements are swapped, the array will at last experience a rotation. To illustrate the above, look below for the case &lt;math&gt;n = 4&lt;/math&gt;</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;pre&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;pre&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4 ... Original Array</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>1,2,3,4 ... Original Array</div></td> </tr> </table> MarSch https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1267721874&oldid=prev MarSch: /* Proof */ include preconditions and proper way to call algorithm, make claim more precise and explain base case 2025-01-06T10:04:55Z <p><span class="autocomment">Proof: </span> include preconditions and proper way to call algorithm, make claim more precise and explain base case</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 10:04, 6 January 2025</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 71:</td> <td colspan="2" class="diff-lineno">Line 71:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>==Proof==</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>==Proof==</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>In this proof, we'll use the<del style="font-weight: bold; text-decoration: none;"> implementation</del> below as Heap's <del style="font-weight: bold; text-decoration: none;">Algorithm</del>. While it is not optimal (it does not minimize moves, which is described in the section below), the implementation is<del style="font-weight: bold; text-decoration: none;"> nevertheless still</del> correct and will produce all permutations<del style="font-weight: bold; text-decoration: none;">. The reason for using the below implementation is that the analysis is easier, and certain patterns can be easily illustrated</del>.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>In this proof, we'll use the below<ins style="font-weight: bold; text-decoration: none;"> implementation</ins> as Heap's <ins style="font-weight: bold; text-decoration: none;">algorithm as it makes the analysis easier, and certain patterns can be easily illustrated</ins>. While it is not optimal (it does not minimize moves, which is described in the section below), the implementation is correct and will produce all permutations.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// Output the k! permutations of A in which the first k elements are permuted in all ways.</div></td> </tr> <tr> <td class="diff-marker"><a class="mw-diff-movedpara-left" title="Paragraph was moved. Click to jump to new location." href="#movedpara_5_4_rhs">&#x26AB;</a></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_4_0_lhs"></a>procedure <del style="font-weight: bold; text-decoration: none;">generate</del>(k : integer, A : array of any):</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// To get all permutations of A, use k := length of A.</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>//</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// If, k &gt; length of A, will try to access A out of bounds.</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>// If k &lt;= 0 there will be no output (empty array has no permutations)</div></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker"><a class="mw-diff-movedpara-right" title="Paragraph was moved. Click to jump to old location." href="#movedpara_4_0_lhs">&#x26AB;</a></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><a name="movedpara_5_4_rhs"></a>procedure <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k : integer, A : array of any):</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k = 1 then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k; i += 1 do</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> for i := 0; i &lt; k; i += 1 do</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> <del style="font-weight: bold; text-decoration: none;">generate</del>(k - 1, A)</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> <ins style="font-weight: bold; text-decoration: none;">permutations</ins>(k - 1, A)</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k is even then</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> if k is even then</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> swap(A[i], A[k-1])</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> swap(A[i], A[k-1])</div></td> </tr> <tr> <td colspan="2" class="diff-lineno">Line 89:</td> <td colspan="2" class="diff-lineno">Line 94:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/syntaxhighlight&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/syntaxhighlight&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array {{mvar|A}} has length {{mvar|n}}, then <del style="font-weight: bold; text-decoration: none;">performing</del> <del style="font-weight: bold; text-decoration: none;">Heap's algorithm</del> will<del style="font-weight: bold; text-decoration: none;"> either</del> result in {{mvar|<del style="font-weight: bold; text-decoration: none;">A</del>}} <del style="font-weight: bold; text-decoration: none;">being</del> <del style="font-weight: bold; text-decoration: none;">"rotated"</del> <del style="font-weight: bold; text-decoration: none;">to</del> <del style="font-weight: bold; text-decoration: none;">the</del> <del style="font-weight: bold; text-decoration: none;">right</del> <del style="font-weight: bold; text-decoration: none;">by</del> <del style="font-weight: bold; text-decoration: none;">1</del> <del style="font-weight: bold; text-decoration: none;">(i.e.</del> <del style="font-weight: bold; text-decoration: none;">each</del> <del style="font-weight: bold; text-decoration: none;">element</del> is <del style="font-weight: bold; text-decoration: none;">shifted</del> to the right <del style="font-weight: bold; text-decoration: none;">with</del> <del style="font-weight: bold; text-decoration: none;">the</del> last element <del style="font-weight: bold; text-decoration: none;">occupying the first position) or result</del> in <del style="font-weight: bold; text-decoration: none;">{{mvar|A}}</del> <del style="font-weight: bold; text-decoration: none;">being</del> <del style="font-weight: bold; text-decoration: none;">unaltered,</del> <del style="font-weight: bold; text-decoration: none;">depending on if {{mvar|n}} is even or odd, respectively</del>.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array {{mvar|A}} has length {{mvar|n}}, then <ins style="font-weight: bold; text-decoration: none;">&lt;code&gt;permutations(n,</ins> <ins style="font-weight: bold; text-decoration: none;">A)&lt;/code&gt;</ins> will result in<ins style="font-weight: bold; text-decoration: none;"> one of two possibilities. If</ins> {{mvar|<ins style="font-weight: bold; text-decoration: none;">n</ins>}} <ins style="font-weight: bold; text-decoration: none;">is</ins> <ins style="font-weight: bold; text-decoration: none;">odd,</ins> <ins style="font-weight: bold; text-decoration: none;">then</ins> <ins style="font-weight: bold; text-decoration: none;">{{mvar|A}}</ins> <ins style="font-weight: bold; text-decoration: none;">is</ins> <ins style="font-weight: bold; text-decoration: none;">unaltered.</ins> <ins style="font-weight: bold; text-decoration: none;">If</ins> <ins style="font-weight: bold; text-decoration: none;">{{mvar|n}}</ins> <ins style="font-weight: bold; text-decoration: none;">is</ins> <ins style="font-weight: bold; text-decoration: none;">even, then {{mvar|A}}</ins> is <ins style="font-weight: bold; text-decoration: none;">"rotated"</ins> to the right <ins style="font-weight: bold; text-decoration: none;">by</ins> <ins style="font-weight: bold; text-decoration: none;">1</ins> <ins style="font-weight: bold; text-decoration: none;">(</ins>last element <ins style="font-weight: bold; text-decoration: none;">shifted</ins> in <ins style="font-weight: bold; text-decoration: none;">front</ins> <ins style="font-weight: bold; text-decoration: none;">of</ins> <ins style="font-weight: bold; text-decoration: none;">other</ins> <ins style="font-weight: bold; text-decoration: none;">elements)</ins>.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td colspan="2" class="diff-empty diff-side-deleted"></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Basis: If array {{mvar|A}} has length 1, then &lt;code&gt;permutations(1, A)&lt;/code&gt; will output A and stop, so A is unaltered. Since 1 is odd, this is what was claimed, so the claim holds.</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Basis: The claim above trivially holds true for &lt;math&gt;n=1&lt;/math&gt; as Heap's algorithm will simply return {{mvar|A}} unaltered in order.</div></td> <td colspan="2" class="diff-empty diff-side-added"></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Induction: Assume the claim holds true for some &lt;math&gt;i \geq 1&lt;/math&gt;. We will then need to handle two cases for &lt;math&gt;i+1&lt;/math&gt;: &lt;math&gt;i+1&lt;/math&gt; is even or odd.</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Induction: Assume the claim holds true for some &lt;math&gt;i \geq 1&lt;/math&gt;. We will then need to handle two cases for &lt;math&gt;i+1&lt;/math&gt;: &lt;math&gt;i+1&lt;/math&gt; is even or odd.</div></td> </tr> </table> MarSch https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1267714403&oldid=prev MarSch: /* Proof */ 2025-01-06T09:00:04Z <p><span class="autocomment">Proof</span></p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 09:00, 6 January 2025</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 89:</td> <td colspan="2" class="diff-lineno">Line 89:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/syntaxhighlight&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;/syntaxhighlight&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array {{mvar|A}} has length {{mvar|n}}, then performing Heap's algorithm will either result in {{mvar|A}} being "rotated" to the right by 1 (i.e. each element is shifted to the right with the last element occupying the first position) or result in {{mvar|A}} being unaltered, depending if {{mvar|n}} is even or odd, respectively.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>Claim: If array {{mvar|A}} has length {{mvar|n}}, then performing Heap's algorithm will either result in {{mvar|A}} being "rotated" to the right by 1 (i.e. each element is shifted to the right with the last element occupying the first position) or result in {{mvar|A}} being unaltered, depending<ins style="font-weight: bold; text-decoration: none;"> on</ins> if {{mvar|n}} is even or odd, respectively.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Basis: The claim above trivially holds true for &lt;math&gt;n=1&lt;/math&gt; as Heap's algorithm will simply return {{mvar|A}} unaltered in order.</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Basis: The claim above trivially holds true for &lt;math&gt;n=1&lt;/math&gt; as Heap's algorithm will simply return {{mvar|A}} unaltered in order.</div></td> </tr> </table> MarSch https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1250041988&oldid=prev 2A01:799:913:BB00:4EFA:7411:67F6:D94A: // minor edit 2024-10-08T04:44:39Z <p>// minor edit</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 04:44, 8 October 2024</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 63:</td> <td colspan="2" class="diff-lineno">Line 63:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i := 1</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i := 1</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> else</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> // Calling generate(i+1, A) has ended as the <del style="font-weight: bold; text-decoration: none;">for</del>-loop terminated. Reset the state and simulate popping the stack by incrementing the pointer.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> // Calling generate(i+1, A) has ended as the <ins style="font-weight: bold; text-decoration: none;">while</ins>-loop terminated. Reset the state and simulate popping the stack by incrementing the pointer.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c[i] := 0</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c[i] := 0</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i += 1</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> i += 1</div></td> </tr> </table> 2A01:799:913:BB00:4EFA:7411:67F6:D94A https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1250041849&oldid=prev 2A01:799:913:BB00:4EFA:7411:67F6:D94A: // minor error in a comment 2024-10-08T04:43:29Z <p>// minor error in a comment</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 04:43, 8 October 2024</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 58:</td> <td colspan="2" class="diff-lineno">Line 58:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end if</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> end if</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> output(A)</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div> // Swap has occurred ending the <del style="font-weight: bold; text-decoration: none;">for</del>-loop. Simulate the increment of the <del style="font-weight: bold; text-decoration: none;">for</del>-loop counter</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div> // Swap has occurred ending the <ins style="font-weight: bold; text-decoration: none;">while</ins>-loop. Simulate the increment of the <ins style="font-weight: bold; text-decoration: none;">while</ins>-loop counter</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c[i] += 1</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> c[i] += 1</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // Simulate recursive call reaching the base case by bringing the pointer to the base case analog in the array</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div> // Simulate recursive call reaching the base case by bringing the pointer to the base case analog in the array</div></td> </tr> </table> 2A01:799:913:BB00:4EFA:7411:67F6:D94A https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1192922896&oldid=prev OAbot: Open access bot: doi updated in citation with #oabot. 2024-01-01T02:25:20Z <p><a href="/wiki/Wikipedia:OABOT" class="mw-redirect" title="Wikipedia:OABOT">Open access bot</a>: doi updated in citation with #oabot.</p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 02:25, 1 January 2024</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 4:</td> <td colspan="2" class="diff-lineno">Line 4:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[File:Wheel diagram Heap's algorithm.svg|thumb|Wheel diagram of all permutations of length &lt;math&gt;n=4&lt;/math&gt; generated by Heap's algorithm, where each permutation is color-coded (1=blue, 2=green, 3=yellow, 4=red).]]</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[File:Wheel diagram Heap's algorithm.svg|thumb|Wheel diagram of all permutations of length &lt;math&gt;n=4&lt;/math&gt; generated by Heap's algorithm, where each permutation is color-coded (1=blue, 2=green, 3=yellow, 4=red).]]</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>'''Heap's [[algorithm]]''' generates all possible [[permutation]]s of {{math|''n''}} objects. It was first proposed by B. R. Heap in 1963.&lt;ref name="Heap"&gt;{{cite journal|last=Heap|first=B. R.|title=Permutations by Interchanges|journal=The Computer Journal|year=1963|volume=6|issue=3|pages=293–4|doi=10.1093/comjnl/6.3.293|doi-access=free}}&lt;/ref&gt; The algorithm minimizes movement: it generates each permutation from the previous one by interchanging a single pair of elements; the other {{math|''n''−2}} elements are not disturbed. In a 1977 review of permutation-generating algorithms, [[Robert Sedgewick (computer scientist)|Robert Sedgewick]] concluded that it was at that time the most effective algorithm for generating permutations by computer.&lt;ref&gt;{{Cite journal | last1 = Sedgewick | first1 = R. | title = Permutation Generation Methods | doi = 10.1145/356689.356692 | journal = ACM Computing Surveys | volume = 9 | issue = 2 | pages = 137–164| year = 1977 | s2cid = 12139332 }}&lt;/ref&gt;</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>'''Heap's [[algorithm]]''' generates all possible [[permutation]]s of {{math|''n''}} objects. It was first proposed by B. R. Heap in 1963.&lt;ref name="Heap"&gt;{{cite journal|last=Heap|first=B. R.|title=Permutations by Interchanges|journal=The Computer Journal|year=1963|volume=6|issue=3|pages=293–4|doi=10.1093/comjnl/6.3.293|doi-access=free}}&lt;/ref&gt; The algorithm minimizes movement: it generates each permutation from the previous one by interchanging a single pair of elements; the other {{math|''n''−2}} elements are not disturbed. In a 1977 review of permutation-generating algorithms, [[Robert Sedgewick (computer scientist)|Robert Sedgewick]] concluded that it was at that time the most effective algorithm for generating permutations by computer.&lt;ref&gt;{{Cite journal | last1 = Sedgewick | first1 = R. | title = Permutation Generation Methods | doi = 10.1145/356689.356692 | journal = ACM Computing Surveys | volume = 9 | issue = 2 | pages = 137–164| year = 1977 | s2cid = 12139332<ins style="font-weight: bold; text-decoration: none;"> | doi-access = free</ins> }}&lt;/ref&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The [[sequence]] of permutations of {{math|''n''}} objects generated by Heap's algorithm is the beginning of the sequence of permutations of {{math|''n''+1}} objects. So there is one infinite sequence of permutations generated by Heap's algorithm {{OEIS|A280318}}.</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The [[sequence]] of permutations of {{math|''n''}} objects generated by Heap's algorithm is the beginning of the sequence of permutations of {{math|''n''+1}} objects. So there is one infinite sequence of permutations generated by Heap's algorithm {{OEIS|A280318}}.</div></td> </tr> </table> OAbot https://en.wikipedia.org/w/index.php?title=Heap%27s_algorithm&diff=1182783418&oldid=prev Kubanczyk: /* Proof */ 2023-10-31T10:35:00Z <p><span class="autocomment">Proof</span></p> <table style="background-color: #fff; color: #202122;" data-mw="interface"> <col class="diff-marker" /> <col class="diff-content" /> <col class="diff-marker" /> <col class="diff-content" /> <tr class="diff-title" lang="en"> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Previous revision</td> <td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 10:35, 31 October 2023</td> </tr><tr> <td colspan="2" class="diff-lineno">Line 71:</td> <td colspan="2" class="diff-lineno">Line 71:</td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br /></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>==Proof==</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>==Proof==</div></td> </tr> <tr> <td class="diff-marker" data-marker="−"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>In this proof, we'll use the implementation below as Heap's Algorithm. While it is not optimal (<del style="font-weight: bold; text-decoration: none;">see</del> section below)<del style="font-weight: bold; text-decoration: none;">{{Clarify|date=March 2020}}</del>, the implementation is nevertheless still correct and will produce all permutations. The reason for using the below implementation is that the analysis is easier, and certain patterns can be easily illustrated.</div></td> <td class="diff-marker" data-marker="+"></td> <td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>In this proof, we'll use the implementation below as Heap's Algorithm. While it is not optimal (<ins style="font-weight: bold; text-decoration: none;">it does not minimize moves, which is described in the</ins> section below), the implementation is nevertheless still correct and will produce all permutations. The reason for using the below implementation is that the analysis is easier, and certain patterns can be easily illustrated.</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>&lt;syntaxhighlight lang="pascal"&gt;</div></td> </tr> <tr> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>procedure generate(k : integer, A : array of any):</div></td> <td class="diff-marker"></td> <td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>procedure generate(k : integer, A : array of any):</div></td> </tr> </table> Kubanczyk