For all n! that generate {1,......,n} The problem of permutation, we can use the reduction method, the size of the problem minus one is to generate All (N-1)! arrangement. Assuming that this small problem has been solved, we can insert n into the possible position of n in each permutation of the n-1 element to get a solution to the larger problem of a large scale. All permutations generated in this way are unique, and their total should be n (n-1)! =n!. In this way, we all have all the permutations of {1,......,n}. Johnsontrotter algorithm implementation form. Johnsontrotter (n) Input: A positive integer n output: An ordered list of {1,......,n} initializes the first permutation to the left element array while there is a moving element K do The largest moving element, K, swaps the neighboring elements of K and its arrows, and reverses the direction of all elements greater than K. Adds a new arrangement to the list (from the basis of algorithm design and analysis) this afternoon, I realized this algorithm, and changed it to It is possible to arrange n non-repeating elements, the comparator used in the program to provide the interface needs to implement itself, the program needs to implement the user's own comparator injection program. Self-perception of procedural flexibility is also possible.
/*** Use the JT algorithm for permutations and combinations. * Note: Be sure to keep the pattern consistent with the comparison interface pattern, otherwise unpredictable errors may occur *@authorliuyefeng<[email protected]> * @date April 9, 2015 PM 5:31:00 * @CopyRight TopView INC *@versionV1.0 *@param<E> the paradigm of the elements that need to be aligned, be sure to keep the paradigm consistent with the comparison interface pattern, otherwise unpredictable errors may occur*/ Public classJtalgorithm<e>{ //An array that holds the permutation elements protectede[] Array; //an array of directions for the element Privatedirection[] Directions; //comparator, for comparing element sizes PrivateCompare<e>Compare; PublicJtalgorithm (class<?extendsCompare<e>>clazz) { //get an instance of a comparison method This. Compare =(Compare) reflectutils.newinstance (clazz); } PublicJtalgorithm (compare<e>Compare) { //get an instance of a comparison method This. Compare =Compare; }
PublicList<e[]>generate (e[] elements) {List<E[]> result =NewArraylist<e[]>(); //Initialization Workinit (elements); //position of the largest movable element intBiggestflag =findbiggestmobileelement (); //itself is also a sort of arrangementResult.add (arrays.copyof (array, array.length)); //existence of movable maximum element K while(Biggestflag! =-1) { //swaps the adjacent elements that the K and arrow point toBiggestflag =Changebiggestelementandneighbor (Biggestflag); //reverses the direction of all elements greater than kchangedirection (Biggestflag); //add a new arrangement to a result setResult.add (arrays.copyof (array, array.length)); //to re-find the movable maximum elementBiggestflag =findbiggestmobileelement (); } returnResult }
Private voidinit (e[] elements) {//sort the elements with a quick linequicksort<e> qk =NewQuicksort<e>(Compare); Qk.sort (Elements,0, Elements.length-1); Array=elements; Directions=NewDirection[array.length]; //Initialization Direction for(inti = 0; i < directions.length; i++) {Directions[i]=Direction.left; } }
/*** Reverse the direction of elements larger than Loc's position *@paramLoc*/ Private voidChangedirection (intLoc) { for(inti = 0; i < Array.Length; i++) { if(Compare.greaterthan (Array[i], Array[loc])) {Directions[i]= (Directions[i] = = direction.left)?Direction.RIGHT:Direction.LEFT; } } }
/*** Swap the LOC element with its neighbors and return the new location of the interchange to LOC *@paramLoc *@returnnew location of Loc*/ Private intChangebiggestelementandneighbor (intLoc) { intNeighbor =-1; if(Directions[loc] = =direction.left) {neighbor= Loc-1; } Else{Neighbor= loc + 1; } E Temp=Array[loc]; Array[loc]=Array[neighbor]; Array[neighbor]=temp; Direction dtemp=Directions[loc]; Directions[loc]=Directions[neighbor]; Directions[neighbor]=dtemp; returnneighbor; }
/*** Find the maximum movable element *@returnposition of the largest movable element*/ Private intfindbiggestmobileelement () {intLOC =-1; intBiggestloc =-1; for(inti = 0; i < Array.Length; i++) { //judging the left and right direction if(Directions[i] = =direction.left) {//if it is a header element, it cannot be compared to the left, skipping if(i = = 0) { Continue; } if(Compare.greaterthan (Array[i], array[i-1]) {loc=i; } } Else { //if it is a trailing element, it cannot be compared to the right, skipping if(i = = Array.length-1) { Continue; } if(Compare.greaterthan (Array[i], array[i + 1]) {loc=i; } } //If you find a movable element for the first time, change the maximum movable element, and then compare the movable element and the largest movable element found. if(loc! =-1 && biggestloc = =-1) {Biggestloc=Loc; }Else if(Biggestloc! =-1 &&Compare.greaterthan (Array[loc], Array[biggestloc])) {Biggestloc=Loc; } } returnBiggestloc; }}
Johnson-trotter (JT) algorithm generation arrangement