Content deleted Content added
Pengfeituan (talk | contribs) |
Pengfeituan (talk | contribs) |
||
Line 3:
Suppose we have a sequence of different characters with a length of N, Heap found that, we can simply interchanges the positions of two elements to get a new permutation output. Let us describe it in a recursive way. If we have got (N-1)! permutation outputs, fixing the last element. Then if N is odd, we can switch the first element and the last one, while N is even we can switch the ith (i is the step number of the cycle, and now it is 1) element and the last one, then we will continue outputting the (N-1)! permutation outputs and switching step for another N-1 times( N times int total). Below is a C/C++ like code.<br>
<code>
void generate(int N,char data[])<br>
{{spaces|4}}if(N==1){output(); return;}<br>
{{spaces|4}}int c;<br>
{{spaces|4}}for(c=1;c<=N;c++)<br>
{{spaces|4}}{<br>
{{spaces|8}}generate(N-1);<br>
{{spaces|8}}swap(data[N%2?1:c],data[N]);<br>
{{spaces|8}}output();<br>
{{spaces|4}}}<br>
</code>
|