I want to find the full arrangement of any set
Solution: assume that the data is stored in the array [0, 1 ..., in length-1], the number of first = 0 is fixed first, and array [1 ,..., length-1], and so on. When first = length-1 is, the data in the array is output. When array [1 ,..., after the length-1] is fully arranged (first = 0 at this time), you need to set the subscript to 1 ,..., all numbers of length-1 are exchanged with first, that is, each number in the array must be placed at first = 0, and so on.
Specific algorithms (Java version)
1 // exchange data marked as M and N in the array. 2 public static void swap (INT [] array, int M, int N) {3 int x = array [m]; 4 array [m] = array [N]; 5 array [N] = X; 6} 7 8 // output data in the array 9 public static void output (INT [] array) {10 for (INT I = 0; I <array. length; I ++) {11 system. out. print (array [I] + "\ t"); 12} 13 system. out. println (); 14} 15 16 public static void permutation (INT [] array, int first) {17 if (first = array. length-1) {// first has reached the last element, that is, all elements have fixed 18 output (array ); 19} else {20 for (INT I = first; I <array. length; I ++) {21 swap (array, I, first); // exchange 22 permutation (array, first + 1); // The previous elements of first are fixed, next, fix the first + 123 swap (array, I, first); // restore 24} 25} 26} 27 28 public static void main (string [] ARGs) {29 int [] array = {1, 2, 3}; // 30 permutation (array, 0); 31}
Arrange the most detailed report