Full arrangement is a time complexity: O (n !) I gave a lecture to the students two days ago and accidentally thought about this question. I came back and summarized that seven algorithms can be used to solve the problem. Among them, dynamic loops are similar to backtracking algorithms, which are cumbersome to implement, therefore, we have summarized six types to serve readers. All algorithms are written in JavaScript and can be run directly.
Algorithm 1: Exchange (recursion)
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Full Permutation (Recursive Swap)-Mengliao Software </title>
</Head>
<Body>
<P> Full Permutation (Recursive Swap) <br/>
Mengliao Software Studio-Bosun Network Co., Ltd. <br/>
2011.05.24 </p>
<Script type = "text/javascript">
/*
Full Permutation (recursive switching) Algorithm
1. place different elements in the first position;
2. Arrange the remaining positions in full (recursion );
3. Recursive exit is to arrange only one element in full.
*/
Function swap (arr, I, j ){
If (I! = J ){
Var temp = arr [I];
Arr [I] = arr [j];
Arr [j] = temp;
}
}
Var count = 0;
Function show (arr ){
Document. write ("P <sub>" ++ count + "</sub>:" + arr + "<br/> ");
}
Function perm (arr ){
(Function fn (n) {// select an element for the nth position
For (var I = n; I <arr. length; I ++ ){
Swap (arr, I, n );
If (n + 1 <arr. length-1) // determine whether the remaining elements in the array to be fully arranged are greater than 1
Fn (n + 1); // sort all the subscripts from n + 1
Else
Show (arr); // displays a set of results
Swap (arr, I, n );
}
}) (0 );
}
Perm (["e1", "e2", "e3", "e4"]);
</Script>
</Body>
</Html>
Algorithm 2: Link (recursion)
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Full Permutation (Recursive Link)-Mengliao Software </title>
</Head>
<Body>
<P> Full Permutation (Recursive Link) <br/>
Mengliao Software Studio-Bosun Network Co., Ltd. <br/>
2012.03.29 </p>
<Script type = "text/javascript">
/*
Full Permutation (recursive link) Algorithm
1. Set the source array as the input array, and the result array to store the arrangement results (initialized as an empty array );
2. link each element of the source array to the result array one by one (to generate a new array object );
3. Delete the linked element from the original array (generate a new array object );
4. recursively call steps 2 and 3 using the new source array and result array as parameters until the source array is empty, an arrangement is output.
*/
Var count = 0;
Function show (arr ){
Document. write ("P <sub>" ++ count + "</sub>:" + arr + "<br/> ");
}
Function perm (arr ){
(Function fn (source, result ){
If (source. length = 0)
Show (result );
Else
For (var I = 0; I <source. length; I ++)
Fn (source. slice (0, I). concat (source. slice (I + 1), result. concat (source [I]);
}) (Arr, []);
}
Perm (["e1", "e2", "e3", "e4"]);
</Script>
</Body>
</Html>
Algorithm 3: backtracking (recursion)
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Full Permutation (Recursive Backtrack)-Mengliao Software </title>
</Head>
<Body>
<P> Full Permutation (Recursive Backtrack) <br/>
Mengliao Software Studio-Bosun Network Co., Ltd. <br/>
2012.03.29 </p>
<Script type = "text/javascript">
/*
Full Permutation (Recursive Backtracking) Algorithm
1. Create a location array, that is, arrange the positions, and convert them to the arrangement of elements;
2. Create a recursive function to search for the nth position;
3. The method for searching the nth position is similar to that for the eighth queen.
*/
Var count = 0;
Function show (arr ){
Document. write ("P <sub>" ++ count + "</sub>:" + arr + "<br/> ");
}
Function seek (index, n ){
If (n> = 0) // you can determine whether the data has been traced back to the first position.
If (index [n] <index. length-1) {// You can select another location.
Index [n] ++; // select the next location
If (function () {// this anonymous function determines whether the location has been selected
For (var I = 0; I <n; I ++)
If (index [I] = index [n]) return true; // selected
Return false; // not selected
})())
Return seek (index, n); // locate the location again
Else
Return true; // find
}
Else {// No position currently available for Recursive Backtracking
Index [n] =-1; // cancel the current location
If (seek (index, n-1) // continue to find the previous position
Return seek (index, n); // find the current location again
Else
Return false; // optional if no position exists
}
Else
Return false;
}
Function perm (arr ){
Var index = new Array (arr. length );
For (var I = 0; I <index. length; I ++)
Index [I] =-1; // all initialization locations are-1, so that the value after ++ is 0.
For (I = 0; I <index. length-1; I ++)
Seek (index, I); // first search n-1-1 Location
While (seek (index, index. length-1) {// continuously search for the nth position, that is, locate all positions
Var temp = [];
For (I = 0; I <index. length; I ++) // converts a position to an element.
Temp. push (arr [index [I]);
Show (temp );
}
}
Perm (["e1", "e2", "e3", "e4"]);
</Script>
</Body>
</Html>
Algorithm 4: backtracking (non-recursion)
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Full Permutation (Non-recursive Backtrack)-Mengliao Software </title>
</Head>
<Body>
<P>
Full Permutation (Non-recursive Backtrack) <br/>
Mengliao Software Studio-Bosun Network Co., Ltd. <br/>
2012.03.29 </p>
<Script type = "text/javascript">
/*
Full Permutation (non-Recursive Backtracking) Algorithm
1. Create a location array, that is, arrange the positions, and convert them to the arrangement of elements;
2. The method for searching the nth position is similar to that for the eighth queen.
*/
Var count = 0;
Function show (arr ){
Document. write ("P <sub>" ++ count + "</sub>:" + arr + "<br/> ");
}
Function seek (index, n ){
Var flag = false, m = n; // flag is used to locate the marker of the position arrangement.
Do {
Index [n] ++;
If (index [n] = index. length) // No location is available
Index [n --] =-1; // reset the current position and roll back to the previous position.
Else if (! (Function (){
For (var I = 0; I <n; I ++)
If (index [I] = index [n]) return true;
Return false;
}) () // This location is not selected
If (m = n) // The current position is searched
Flag = true;
Else
N ++;
} While (! Flag & n> = 0)
Return flag;
}
Function perm (arr ){
Var index = new Array (arr. length );
For (var I = 0; I <index. length; I ++)
Index [I] =-1;
For (I = 0; I <index. length-1; I ++)
Seek (index, I );
While (seek (index, index. length-1 )){
Var temp = [];
For (I = 0; I <index. length; I ++)
Temp. push (arr [index [I]);
Show (temp );
}
}
Perm (["e1", "e2", "e3", "e4"]);
</Script>
</Body>
</Html>
Algorithm 5: Sorting (non-recursive)
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Full Permutation (Non-recursive Sort)-Mengliao Software </title>
</Head>
<Body>
<P>
Full Permutation (Non-recursive Sort) <br/>
Mengliao Software Studio-Bosun Network Co., Ltd. <br/>
2012.03.30 </p>
<Script type = "text/javascript">
/*
Full Permutation (non-recursive order) Algorithm
1. Create a location array, that is, arrange the positions, and convert them to the arrangement of elements;
2. Perform full sorting according to the following algorithms:
Set P to 1 ~ A full arrangement of n (Location Number): p = p1, p2... pn = p1, p2... PJ-1, pj, pj + 1... pk-1, pk, pk + 1... pn
(1) from the end of the arrangement, find the first index j, which is smaller than the number on the right (j starts from the header ), that is, j = max {I | pi <pi + 1}
(2) In the position number on the Right of pj, find all the INDEX k with the smallest position number greater than pj, that is, k = max {I | pi> pj}
The position number on the Right of pj increases progressively from right to left, so k is the largest index among all the position numbers greater than pj.
(3) Switch pj and pk
(4) then pj + 1... pk-1, pk, pk + 1... pn flipped to get the P' = p1, p2... PJ-1, pj, pn... pk + 1, pk, pk-1... pj + 1
(5) P' is the next arrangement of p.
For example:
24310 is the position number 0 ~ 4. The following steps are taken to find the next arrangement:
(1) from the right to the left, find the first number that is smaller than the number on the right. 2;
(2) Find the smallest 3 of the two digits after the number;
(3) Exchange 2 and 3 for 34210;
(4) flip all the numbers behind the original 2 (current 3), that is, flip 4210 to 30124;
(5) The next order of 24310 is 30124.
*/
Var count = 0;
Function show (arr ){
Document. write ("P <sub>" ++ count + "</sub>:" + arr + "<br/> ");
}
Function swap (arr, I, j ){
Var t = arr [I];
Arr [I] = arr [j];
Arr [j] = t;
}
Function sort (index ){
For (var j = index. length-2; j> = 0 & index [j]> index [j + 1]; j --)
; // This cycle starts from the end of the position array and finds the position with the first left less than the right, that is, j
If (j <0) return false; // All sorted
For (var k = index. length-1; index [k] <index [j]; k --)
; // This cycle starts from the end of the position array and finds the smallest position greater than the j position, that is, k
Swap (index, j, k );
For (j = j + 1, k = index. length-1; j <k; j ++, k --)
Swap (index, j, k); // in this loop, flip all positions from j + 1 to the end
Return true;
}
Function perm (arr ){
Var index = new Array (arr. length );
For (var I = 0; I <index. length; I ++)
Index [I] = I;
Do {
Var temp = [];
For (I = 0; I <index. length; I ++)
Temp. push (arr [index [I]);
Show (temp );
} While (sort (index ));
}
Perm (["e1", "e2", "e3", "e4"]);
</Script>
</Body>
</Html>
Algorithm 6: modulus (non-recursion)
Copy codeThe Code is as follows: <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Full Permutation (Non-recursive Modulo)-Mengliao Software </title>
</Head>
<Body>
<P> Full Permutation (Non-recursive Modulo) <br/>
Mengliao Software Studio-Bosun Network Co., Ltd. <br/>
2012.03.29 </p>
<Script type = "text/javascript">
/*
Full Permutation (non-recursive modulo) Algorithm
1. initialize the array result that stores the full-arrangement results, which is equal to the number of elements in the original array;
2. calculate the total number of all n elements, that is, n !;
3. Any integer greater than or equal to 0 starts to cycle n! Times, accumulating 1 at a time, marked as index;
4. Take the 1st element arr [0] and obtain the expression percentile in the hexadecimal notation, that is, evaluate the value w of index module1, and divide the 1st elements (arr [0]) insert the w position of the result and iterate the index into index \ 1;
5. Take the 2nd element arr [1] and obtain the Binary Expression percentile, that is, evaluate the value w of index mod 2, and divide the 2nd elements (arr [1]) insert the w position of the result and iterate the index into index \ 2;
6. Take the 3rd element arr [2] and obtain the expression percentile of the 3 hexadecimal notation, that is, evaluate the value w of the index mod 3, and divide the 3rd elements (arr [2]) insert the w position of the result and iterate the index into index \ 3;
7 ,......
8. Obtain an arrangement until arr [arr. length-1] is obtained;
9. When the index loop is completed, all the arrays are obtained.
Example:
Find the full arrangement of the four elements ["a", "B", "c", "d"], repeating 4 in total! = 24 times. It can start from any integer index> = 0 and accumulate 1 at a time until the cycle ends after index + 23;
Suppose index = 13 (or 13 + 24, 13 + 2*24, 13 + 3*24 ...), Because there are four elements in total, the process of this arrangement is obtained after four iterations:
1st iterations, 13/1, operator = 13, and remainder = 0. Therefore, 1st elements are inserted at 0th positions (I .e., the subscript is 0). ["a"] is obtained.
For 2nd iterations, 13/2, quotient = 6, and remainder = 1, 2nd elements are inserted at 1st positions (I .e., the subscript is 1). ["a", "B"] is obtained.
3rd iterations, 6/3, quotient = 2, and remainder = 0. Therefore, 3rd elements are inserted at 0th positions (I .e., the subscript is 0). ["c", "", "B"];
4th iterations, 2/4, quotient = 0, remainder = 2, so 4th elements are inserted at 2nd positions (I .e., subscript is 2), get ["c", "", "d", "B"];
*/
Var count = 0;
Function show (arr ){
Document. write ("P <sub>" ++ count + "</sub>:" + arr + "<br/> ");
}
Function perm (arr ){
Var result = new Array (arr. length );
Var fac = 1;
For (var I = 2; I <= arr. length; I ++)
Fac * = I;
For (index = 0; index <fac; index ++ ){
Var t = index;
For (I = 1; I <= arr. length; I ++ ){
Var w = t % I;
For (j = I-1; j> w; j --)
Result [j] = result [j-1];
Result [w] = arr [I-1];
T = Math. floor (t/I );
}
Show (result );
}
}
Perm (["e1", "e2", "e3", "e4"]);
</Script>
</Body>
</Html>
Some of the above six algorithms arrange positions, such as backtracking and sorting, because they can adapt to various types of elements, rather than requiring that the elements to be sorted must be numbers or letters.