Generating permutations
The generation arrangement is the whole arrangement of n number, obviously the time complexity is n-digit O (n^k)
Assuming that you can generate all permutations of the number of n-1, you can extend the arrangement of Generation,....., N.
For example, the 1 generation arrangement is 1
The resulting permutations of 2,1 and
The generation of a-to-be is arranged on the basis of the generation arrangement:
1 in 1th place, 2,3 's generation arrangement
2 in 1th Place, 1,3 's generation arrangement
3 in 1th place, 2,3 's generation arrangement
Then the generation arrangement that is extended to,..., N is:
1 in 1th position, 2,..., N generation arrangement
2 in 2nd place, 1,3,... N generation arrangement
....
N in 1th Place, 2,..., N generation arrangement
First consider the number 1 in the first place, then the 2,,..., N generation arrangement,
Ideas:
1. On the 0 to n-1 position of the number generation arrangement, the practice from 1,,.., N in order to take out the number I (I from 1 to n) and position J (J from 0 to N-1) on the digital exchange, and then the position j+1 to the n-1 position of the number generation arrangement.
2. A number generation arrangement of J (J from 1) to n-1 position, in which the number I (starting from 2) is sequentially taken out from 2,,.., N and the number on position J is exchanged, and the number generated in position j+1 to n-1 position is arranged.
3 .... When j = N, generates an arrangement and prints
4. Retrace the previous step, take the number of words i+1 and J on the digital exchange, and the sequence after the j+1 to generate a sort
5. Until I>n, end
#include <iostream> using namespace std; void permutation (int* array, int ilength, int icurstep) {if (ilength = = icurstep)//If fixed position pointer refers to the last digit printed array {for (int i=0;i< ilength;i++) {cout<<array[i]<< "";} cout<< "\ n"; return;} else{for (int i = icurstep; i<ilength; i++) //i is the scan pointer from the fixed pointer position to the last digit position {swap (array[icurstep],array[i]); Fixed the number from each position from Icurstep to Icurstep on permutation (array,ilength,icurstep+1); swap (Array[i],array[icurstep]); Recover array}} int main () { int array[] = {1,2,3,4}; Permutation (array, 4, 0); }
Algorithm note generation arrangement of 03--inductive method