Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I actually wrote an algorithm for this by hand once. Here it is:</p> <pre><code>bool incr(int z[NUM_INDICES]){ int a=NUM_INDICES-1; for(int i=NUM_INDICES-2;i&gt;=0;i--) if(z[i]&gt;z[i+1]) a--; else break; if(a==0) return false; int b=2147483647,c; for(int i=a;i&lt;=NUM_INDICES-1;i++) if(z[i]&gt;z[a-1]&amp;&amp;z[i]-z[a-1]&lt;b){ b=z[i]-z[a-1]; c=i; } int temp=z[a-1]; z[a-1]=z[c]; z[c]=temp; qsort(z+a,NUM_INDICES-a,sizeof(int),comp); return true; } </code></pre> <p>This is the increment function (i.e. you have an array like [3,2,4,1], you pass it to this, and it modifies it to [3,4,1,2]). It works off the fact that if the last <em>d</em> elements of the array are in descending order, then the next array (in "alphabetical" order) should satisfy the following conditions: 1) the last <em>d+1</em> elements are a permutation among themselves; 2) the <em>d+1</em>-th to last element is the next highest element in the last <em>d+1</em> elements; 3) the last <em>d</em> elements should be in ascending order. You can see this intuitively when you have something like [2,5,3, 8,7,6,4,1]: <em>d</em> = 5 in this case; the 3 turns into the next highest of the last <em>d+1</em> = 6 elements; and the last <em>d</em> = 5 are arranged in ascending order, so it becomes [2,5,4, 1,3,6,7,8].</p> <p>The first loop basically determines <em>d</em>. It loops over the array backwards, comparing consecutive elements, to determine the number of elements at the end that are in descending order. At the end of the loop, <code>a</code> becomes the first element that is in the descending order sequence. If <code>a==0</code>, then the whole array is in descending order and nothing more can be done. </p> <p>The next loop determines what the <em>d+1</em>-th-to-last element should be. We specified that it should be the next highest element in the last <em>d+1</em> elements, so this loop determines what that is. (Note that z[a-1] is the <em>d+1</em>-th-to-last element.) By the end of that loop, <code>b</code> contains the lowest <code>z[i]-z[a-1]</code> that is positive; that is, <code>z[i]</code> should be greater than <code>z[a-1]</code>, but as low as possible (so that <code>z[a-1]</code> becomes the next highest element). <code>c</code> contains the index of the corresponding element. We discard <code>b</code> because we only need the index.</p> <p>The next three lines swap <code>z[a-1]</code> and <code>z[c]</code>, so that the <em>d+1</em>-th-to-last element gets the element next in line, and the other element (<code>z[c]</code>) gets to keep <code>z[a-1]</code>. Finally, we sort the last <em>d</em> elements using <code>qsort</code> (<code>comp</code> must be declared elsewhere; see C++ documentation on <code>qsort</code>). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload