Full arrangement is a time complexity of: O (n!) The algorithm, the first two days to give lectures to students, inadvertently think of this problem, come back summed up, can be solved by 7 kinds of algorithms, which the dynamic cycle similar backtracking algorithm, to achieve more cumbersome, so summed up 6 kinds of readers. All algorithms are written using JavaScript and can be run directly.
algorithm One: Switching (recursive)
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>full permutation (recursive Swap)-Mengliao software</title>
<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 the first position each different element;
2, the remaining position of the full arrangement (recursion);
3, the recursive export to only one element of the full arrangement.
*/
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 element for nth position
for (Var i=n;i<arr.length;i++) {
Swap (ARR,I,N);
if (n+1<arr.length-1)//Determines whether the remaining elements in the array to be fully arranged are greater than 1
FN (n+1); Full arrangement from n+1 subscript
Else
Show (arr); Display a set of results
Swap (ARR,I,N);
}
}) (0);
}
Perm (["E1", "E2", "E3", "E4"]);
</script>
</body>
algorithm two: Link (recursive)
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>full permutation (recursive Link)-Mengliao software</title>
<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 input array, the result array holds the permutation result (initialize to empty array);
2. Link each element of the source array to the result array one by one (generate a new array object);
3. Delete the linked elements from the original array (generate a new array object);
4, the new source array and the result array as parameters recursive call steps 2, 3, until the source array is empty, output an arrangement.
*/
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>
algorithm Three: Backtracking (recursive)
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>full permutation (recursive Backtrack)-Mengliao software</title>
<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, the establishment of the location of the array, that is, the location of the arrangement, after the success of the arrangement to convert to elements;
2, the establishment of recursive functions, to search the nth position;
3, the nth position search method and eight Queens problem similar.
*/
var count = 0;
Function Show (arr) {
document.write ("p<sub>" + ++count + "</SUB>:" + arr + "<br/>");
}
function Seek (index, n) {
if (n >= 0)/////////////////
if (Index[n] < index.length-1) {//There is a next location to choose from
index[n]++; Select Next Location
if (function () {//The anonymous function determines whether the location has been selected
for (var i = 0; i < n; i++)
if (index[i] = = Index[n]) return true; Has been selected
return false; Not selected
})())
Return Seek (index, n); Find a place again
Else
return true; Found it
}
else {//currently no position optional, recursive backtracking
Index[n] =-1; Cancel Current Position
if (Seek (index, n-1))//Continue to find the previous location
Return Seek (index, n); Re-find the current position
Else
return false; There is no location to choose from
}
Else
return false;
}
function Perm (arr) {
var index = new Array (arr.length);
for (var i = 0; i < index.length; i++)
Index[i] =-1; Initialize all positions to-1 so that + + is followed by 0
for (i = 0; i < index.length-1; i++)
Seek (index, i); Search the front n-1 position first
while (Seek (index, index.length-1)) {//Search the nth position continuously, find all position arrangement
var temp = [];
for (i = 0; i < index.length i++)//convert position to Element
Temp.push (Arr[index[i]]);
Show (temp);
}
}
Perm (["E1", "E2", "E3", "E4"]);
</script>
</body>
algorithm Four: backtracking (non-recursive)
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>full permutation (non-recursive Backtrack)-Mengliao software</title>
<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, the establishment of the location of the array, that is, the location of the arrangement, after the success of the arrangement to convert to elements;
2, the nth position search method and eight Queens problem similar.
*/
var count = 0;
Function Show (arr) {
document.write ("p<sub>" + ++count + "</SUB>:" + arr + "<br/>");
}
function Seek (index, n) {
var flag = false, M = n; Flag to find the location of the label, M save which location is being searched
do {
index[n]++;
if (index[n] = = index.length)//No location available
index[n--] =-1; Reset current position, back to previous position
else if (! ( function () {
for (var i = 0; i < n; i++)
if (index[i] = = Index[n]) return true;
return false;
}) ()///The location is not selected
if (M = = N)//Current position Search complete
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>
algorithm Five: Sort (non-recursive)
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>full permutation (non-recursive Sort)-Mengliao software</title>
<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, the establishment of the location of the array, that is, the location of the arrangement, after the success of the arrangement to convert to elements;
2, according to the following algorithm for the ranking:
Set p to be a full arrangement of 1~n (position number): p = p1,p2...pn = P1,p2...pj-1,pj,pj+1...pk-1,pk,pk+1...pn
(1) Starting from the end of the arrangement, find the first index that is smaller than the right position number J (J from the beginning), that is, j = max{i | Pi < pi+1}
(2) In the position number on the right of the PJ, find the index k of the smallest position number in the position number that is larger than PJ, that is, k = max{i | pi > PJ}
The position number on the right side of PJ is incremented from right to left, so K is the highest index in any position number larger than PJ.
(3) Exchange PJ and PK
(4) Pj+1...pk-1,pk,pk+1...pn flip to get arrange p ' = p1,p2...pj-1,pj,pn...pk+1,pk,pk-1...pj+1
(5) P ' is the next permutation of the permutation p
For example:
24310 is an arrangement of the position number 0~4, and the next step is to arrange the following:
(1) from the right to the left to find the first number of permutations than the right number 2;
(2) Find the smallest 3 of the number in the figure after the number 2;
(3) Exchanging 2 with 3 to get 34210;
(4) The original 2 (current 3) after all the numbers, that is, flip 4210, get 30124;
(5) The next permutation 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 loop starts at the end of the location array and finds the first left less than the right position, that is, J
if (J < 0) return false; All arrangements completed
for (var k = index.length-1 Index[k] < index[j]; k--)
; This loop starts at the end of the array of positions and finds the smallest of positions larger 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); This loop flips j+1 to the end of all positions
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>
algorithm VI: modulo (not recursive)
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>full permutation (non-recursive modulo)-Mengliao software</title>
<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 of the whole arrangement, equal to the number of elements of the original array;
2, calculate the total number of n elements, that is, n!;
3, from the >=0 of any integer start cycle n! times, each cumulative 1, recorded as index;
4, take the 1th element arr[0], for 1 of the expression of the lowest, that is, the value of the index modulo 1 W, the 1th element (Arr[0]) inserted in the W position of result, and the index index\1;
5, take the 2nd element arr[1], for 2 of the expression of the lowest, that is, the value of the index modulo 2 W, the 2nd element (Arr[1]) inserted in the W position of result, and the index index\2;
6, take the 3rd element Arr[2], for 3 of the expression of the lowest, that is, the value of the index modulo 3 W, the 3rd element (Arr[2]) inserted in the W position of result, and the index index\3;
7 、......
8, until the last element Arr[arr.length-1], at this time to obtain an arrangement;
9. When the index loop is complete, all permutations are obtained.
Cases:
4 Elements ["A", "B", "C", "D"] of the full arrangement, a total of cyclic 4!=24, can be from any >=0 of the integer index start loop, each cumulative 1, until the end of the cycle index+23;
Suppose index=13 (or 13+24,13+2*24,13+3*24 ...). , because there are 4 elements, so the iteration is 4 times, the resulting arrangement is:
1th iteration, 13/1, quotient =13, remainder = 0, so the 1th element inserts No. 0 position (that is subscript 0), get ["a"];
2nd iteration, 13/2, quotient =6, remainder = 1, so the 2nd element inserts 1th position (that is subscript 1), get ["A", "B"];
3rd iteration, 6/3, quotient =2, remainder = 0, so the 3rd element inserts No. 0 position (namely Subscript 0), get ["C", "A", "B"];
4th iteration, 2/4, quotient =0, remainder = 2, so the 4th element inserts 2nd position (namely Subscript 2), get ["C", "a", "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>
Some of the above six algorithms arrange locations, such as backtracking, sorting, and so on, because they can accommodate various types of elements, rather than requiring that the elements to be sorted must be numbers or letters.