Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating all n-letter permutations
    text
    copied!<p>I am attempting to calculate all the possible 3 letter <em>permutations</em>, using the 26 letters (Which amounts to only 26*25*24=15,600). The order of the letters matters, and I don't want repeating letters. (I wanted the permutations to be generated in lexicographical order, but that isn't necessary)</p> <p>So far I attempted to nest <code>for</code> loops, but I ended up iterating through every <em>combination</em> possible. So there are repeating letters, which I do not want, and the <code>for</code> loops can become difficult to manage if I want more than 3 letters.</p> <p>I can flip through the letters until I get a letter that has not been used, but it isn't in lexicographical order and it is <em>much</em> slower than using <code>next_permutation</code> (I cannot use this <code>std</code> method because I'm left calculating all of the subsets of the 26 letters). </p> <p>Is there a more efficient way to do this? To put in perspective of the inefficiency, <code>next_permutation</code> iterates through the first 6 digits instantaneously. However, it takes several seconds to get all the three letter permutations using this method, and <code>next_permutation</code> still quickly becomes inefficient with the 2^n subsets I must calculate.</p> <p>Here is what I have for the nested <code>for</code> loops:</p> <pre><code>char key[] = {'a','b','c','d','e','f','g','h','i','j','k', 'l','m','n','o','p','r','s','t','u','v','w','x','y','z'}; bool used[25]; ZeroMemory( used, sizeof(bool)*25 ); for( int i = 0; i &lt; 25; i++ ) { while( used[i] == true ) i++; if( i &gt;= 25 ) break; used[i] = true; for( int j = 0; j &lt; 25; j++ ) { while( used[j] == true ) j++; if( j &gt;= 25 ) break; used[j] = true; for( int k = 0; k &lt; 25; k++ ) { while( used[k] == true ) k++; if( k &gt;= 25 ) break; used[k] = true; cout &lt;&lt; key[i] &lt;&lt; key[j] &lt;&lt; key[k] &lt;&lt; endl; used[k] = false; } used[j] = false; } used[i] = false; } </code></pre>
 

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