JS achieve full array

https://www.jb51.net/article/39291.htm

 

Six kinds of algorithms JavaScript full array realization

A algorithm: exchange (recursive)

Copy the code code is as follows:

<HTML xmlns = "the http://www.w3.org/1999/xhtml">  
<head>  
    <Meta HTTP-equiv = "the Type-the Content" Content = "text / HTML; charset = UTF-. 8" />  
    < title> Full Permutation (the Recursive Swap) - Mengliao Software </ title>  
</ head>  
<body>  
<P> Full Permutation (the Recursive Swap) <br />  
Mengliao Software Studio - Bosun the Network Co., Ltd.'s <br />  
2011.05.24 </ P>  
<Script type = "text / JavaScript">  
/ *  
full array (recursively exchange) algorithm  
1, the first position placing various elements, respectively;  
2, the remaining position of full alignment ( recursive);  
3, only one recursive export full array elements.  
* / 
Function the swap (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) {// n-th position selected elements  
        for (var i = n; i   <arr.length; i ++) {
            the swap (ARR, I, n-);  
            iF (n +. 1 <-arr.length. 1) // full array element is determined to be remaining in the array is greater than 1  
                fn (n + 1); // from n + a full array subscripts for  
            the else 
                Show (ARR); // display a set of results of  
            the swap (ARR, I, n-);  
        }  
    }) (0);  
}  
perm(["e1","e2","e3","e4"]);  
</script>  
</body>  
</html> 


Algorithms II: Link (recursive)

Copy the code code is as follows:



















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> 


Algorithms three: back (recursively)

Copy the code code is as follows:

<HTML xmlns = "the http://www.w3.org/1999/xhtml">  
<head>  
    <Meta HTTP-equiv = "the Type-the Content" Content = "text / HTML; charset = UTF-. 8" />  
    < title> Full Permutation (the Recursive the Backtrack) - Mengliao Software </ title>  
</ head>  
<body>  
<P> Full Permutation (the Recursive the Backtrack) <br />  
Mengliao Software Studio - Bosun the Network Co., Ltd.'s <br />  
2012.03.29 </ P>  
<Script type = "text / JavaScript">  
/ *  
full array (recursive backtracking) algorithm is  
an established position in the array, i.e., the position is aligned after the success of arrangement conversion elements are arranged;  
2, established recursive function, search for the n-th position;  
3, the n-th search position similar manner to the eight queens problem.  
* / 
Var COUNT = 0;  

    document.write ( "P <Sub>" COUNT + ++ + "</ Sub>:" + + ARR "<br />");  
}  
function Seek (index, n-) {  
    IF (n-> = 0) / before / determines whether back to the first position, i.e., all positions in the array have found  
        if (index [n] <index.length - 1) {// optional position there is a next  
            index [n] ++; / / select the 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; // selected  
                return false; // not selected  
            }) ())  
                return Seek (index, n-); // re-find the position of  
            the else 
                return to true; // find  
        }  
        else {// No current position Alternatively, backtracking recursive  
            index [n] = -1; // cancel the current position  
            if (seek (index, n - 1)) // continue looking for a position on  
                return seek (index, n ); // re-find the current position of  
            the else 
                return to false; // optional position no longer  
        }  
    the else 
        return to false;  
}  
function Perm (ARR) {  
    var index = new new the Array (arr.length);  
    for (var I = 0; I <index.length; I ++)  
        index [I] = -1; -1 // initialize all positions, so after ++ is 0  
    for (I = 0; I <index.length -. 1; I ++)  
        Seek (index, I); // first search before the n-1 position  
    while (seek (index, index.length - 1)) {// keep searching for the n-th position, i.e., find all arranged in positions  
        TEMP = var [];  
        for (I = 0; I <index.length; I ++) // converts the position of an element  
            temp.push (ARR [index [I]]);  
        Show (TEMP);  
    }  
}  
Perm ( [ "E1", "E2", "E3", "E4"]);  
</ Script>  
</ body>  
</ HTML> 


Algorithm four: back (non-recursive)

Copy the code code is as follows:

<HTML xmlns = "the http://www.w3.org/1999/xhtml">  
<head>  
    <Meta HTTP-equiv = "the Type-the Content" Content = "text / HTML; charset = UTF-. 8" />  
    < title> Full Permutation (Non-recursive This the Backtrack) - Mengliao Software </ title>  
</ head>  
<body>  
<P>  
Full Permutation (Non-recursive This the Backtrack) <br />  
Mengliao Software Studio - Bosun the Network Co., Ltd.'s /> <br  
2012.03.29 </ P>  
<Script type = "text / JavaScript">  
/ *  
full array (non-recursive backtracking) algorithm is  
an established position in the array, i.e., the position is arranged, the arrangement of elements successfully converted to arrangement;  
2, the n-th search position similar manner to the eight queens problem.  
* / 
Var COUNT = 0;  

    document.write ( "P <Sub>" COUNT + ++ + "</ Sub>:" + + ARR "<br />");  
}  
function Seek (index, n-) {  
    var = In Flag to false, m = n- ; in flag is a flag arrangement found, m which position is stored // search  
    do {  
        index [n-] ++;  
        IF (index [n-] == index.length) // no longer available location  
            index [n--] = 1; // reset the current position, a fall back position  
        the else IF ((function () {!  
            for (var I = 0; I <n-; I ++)  
                IF (index [I] == index [n- ]) return to true;  
            return to false;  
        }) ()) // this position is not selected  
            if (m == n) // current position of the search is completed  
                In Flag to true =;  
            the 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> 


Algorithms Five: Sort (non-recursive)

Copy the code code is as follows:

<HTML xmlns = "the http://www.w3.org/1999/xhtml">  
<head>  
    <Meta HTTP-equiv = "the Type-the Content" Content = "text / HTML; charset = UTF-. 8" />  
    < title> Full Permutation (Non-recursive This the Sort) - Mengliao Software </ title>  
</ head>  
<body>  
<P>  
Full Permutation (Non-recursive This the Sort) <br />  
Mengliao Software Studio - Bosun the Network Co., Ltd.'s /> <br  
2012.03.30 </ P>  
<Script type = "text / JavaScript">  
/ *  
full array (non-recursive evaluation order) algorithm is  
an established position in the array, i.e., the position is aligned after the success of arrangement conversion element arrangement;  
2, arithmetically perfection arranged as follows:  
Let P 1 ~ n (position number) of a full permutation: p = p1, p2 ... pn = p1, p2 ... pj-1, pj, pj + 1 ... pk-1,   pk, pk + 1 ... pn
(1) beginning from the end of the arrangement, to find the right position of the first number is smaller than the index j (j calculated from the first portion), i.e., = max {I J | PI <PI + 1}  
(2) to the right of the pj position numbers, to find all the smallest position number larger than pj in position number index k, i.e., k = max {i | pi>   pj}
   position number pj right is incremented from right to left, so that k pj all particles larger than the maximum index position number  
(3) and exchanged pj PK  
(. 4) then pj + 1 ... pk-1, pk, pk + 1 ... pn inverted arrangement to obtain p '= p1, p2 PJ. 1-..., PJ, PK ... PN +. 1, PK, PK-+. 1. 1 ... PJ  
(. 5) p 'is arranged next permutation p of  

example:  
24310 is the position number 0 to 4, a permutation, which seek the next step arranged as follows:  
(1) are arranged from right to left to find the first digital right number smaller than 2;  
(2) identify the number after the numbers larger than 2 in a number of minimum 3;  
(3) the exchange 2 and 3 to give 34210;  
(4) all the 2 original numbers (currently 3) inverting back, i.e. flipped 4210 to obtain 30124;  
(5) obtained at the 24310 It is arranged in a 30124.  
* / 
Var COUNT = 0;  
function Show (ARR) {  
    document.write ( "P <Sub>" COUNT + ++ + "</ Sub>:" + + ARR "<br />");  
}  
function the swap (ARR, I, J) {  
    var ARR T = [I ];  
    ARR [I] = ARR [J];  
    ARR [J] = T;  

}  
function Sort (index) {  
    for (var index.length J = - 2; J> = 0 && index [J]> index [J . 1 +]; J,)  
        ; // this cycle start position from the end of the array, is less than the left to find the right position of the first, i.e. J  
    IF (J <0) return to false; // has completed all arranged  
    for (var index.length = K -. 1; index [K] <index [j]; K--)  
        ; // start from the end of the present cycle of position in the array, to find the position j is larger than the minimum position, i.e., K  
    the swap (index , J, K);  
    for (J = J +. 1, K = index.length -. 1; J <K; J ++,   K--)
        the swap (index, J, K); // this inversion cycle 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>  
</html> 


Algorithm six: mod (non-recursive)

Copy the code code is as follows:

<HTML xmlns = "the http://www.w3.org/1999/xhtml">  
<head>  
    <Meta HTTP-equiv = "the Type-the Content" Content = "text / HTML; charset = UTF-. 8" />  
    < title> Full Permutation (Non-recursive This Modulo) - Mengliao Software </ title>  
</ head>  
<body>  
<P> Full Permutation (Non-recursive This Modulo) <br />  
Mengliao Software Studio - Bosun the Network Co., Ltd.'s /> <br  
2012.03.29 </ P>  
<Script type = "text / JavaScript">  
/ *  
full array (modulo non-recursive) algorithm is  
the number of elements 1, arranged to initialize the whole storage array result result, the original array equal;  
2, calculating the total number of full array of n elements, i.e. !; n  
. 3, from> 0 is an arbitrary integer cycle begins = n!   Times, each time incremented by 1, referred to as index;
. 4, taking the first element arr [0], seeking the lowest 1 bit binary expression, that is to finding the index modulus values of w 1, the first element (arr [0 ]) w insertion position of the result, and the index of the iteration index \. 1;  
5, taking the second element arr [1], seeking the lowest bit binary expression, that is to finding modulus index value w is 2, the second element (arr [1]) w inserted position of the result, and the index iteration index \ 2;  
. 6, take the first three elements arr [2], for the three lowest bit hexadecimal expression, i.e., seeking index modulus value W 3 of the third element (arr [2]) of the inserted result w position, and the index of the iteration index \. 3;  
. 7, ......  
. 8, until the last element taken arr [arr.length-1], this time a determined arrangement;  
9, when the index cycle is completed, all the obtained arrangement.  

Example:  
find elements 4 [ "a", "b" , "c", "d"] of the full array, a total of 4 cycles = 24, available from any> cycle started 0 = integer index of each accumulated! 1, until the loop 23 after the completion of end index +;  
hypothesis index = 13 (or 13 + 24, 13 + 24, 13 + 2 * 24 * 3 ...), because a total of four elements, so iteration 4, this is obtained a permutation process is:  
1st iteration, 13/1, commercially = 13, the remainder = 0, so that the first element is inserted into the 0-th position (i.e., subscript 0), to give [ "a"];  
2 iterations, 13/2, commercially = 6, remainder = 1, so the second element into the first position (i.e., subscript 1), to give [ "a", "b"   ];
the third iteration, 6 / 3, and Suppliers = 2, remainder = 0, so that the third element is inserted into the 0-th position (i.e., subscript 0), to give [ "c", "a"   , "b"];
4th iteration 2 / 4, commercially = 0, 2 = remainder,  

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 algorithm is six positions arranged, e.g. backtracking, sorting, etc., because it can accommodate various types of elements, not necessarily required to be arranged elements like numbers or letters.

Reproduced in: https: //www.cnblogs.com/leftJS/p/11073294.html

Guess you like

Origin blog.csdn.net/weixin_33834679/article/details/93722616