PHP full sorting algorithm implements program code. PHP full sorting algorithm implements program code to take m (mn) elements from n different elements and arrange them in a certain order, it is called an arrangement for retrieving m elements from n different elements. When PHP fully-arranged algorithms implement program code
M (m ≤ n) elements from n different elements are arranged in a certain order, which is called an arrangement of m elements from n different elements. When m = n, all data is arranged in full order.
Introduction
For example, columns 1, 2, and 3 in the full row:
1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1
A total of 3*2*1 = 6 3!
2 Formula
Number of fully arranged items f (n) = n! (Definition 0! = 1)
Recursive Algorithms
1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 2, 1
3, 1, 2
This is because the algorithm only considers how to output a full arrangement, but does not consider whether there is a problem with the transposition. So I proposed a solution, which is to modify the transposition function.
For example, if you change the positions of 1 2 3, you should not directly change the positions of 3 2 1, so that 3 and 1 are directly changed. Instead, 3 is placed at the front and back, and 1 2 is placed backward.
Basic Algorithms
The following describes four full-rank algorithms:
(A) lexicographic method
(B) incremental carry-in method
(C) decimal carry-in method
(D) adjacent-position swap method
Implement a full sorting algorithm
| The code is as follows: |
|
|
Header ("content-type: text/html; charset = utf-8 ");/**
* @ Param array $ a set of elements to be arranged, which dynamically changes
* @ Param array $ B stores the current arrangement
* @ Param array $ M the set of elements to be arranged. it is equivalent to a constant and always the set of elements to be sorted.
*/
Function wholerange ($ a, $ B, $ M ){
$ Range = array ();
If (count ($ a)> 1 ){
$ D = $ B;
Foreach ($ a as $ value ){
$ B [] = $ value;
$ C = array_diff ($ M, $ B );
If (count ($ c)> 0 ){
$ Range [] = wholerange ($ c, $ B, $ M );
}
$ B = $ d;
}
} Elseif (count ($ a) = 1 ){
Foreach ($ a as $ value ){
$ B [] = $ value;
}
$ Onerange = "";
Foreach ($ B as $ value ){
$ Onerange. = $ value;
}
$ Range [] = $ onerange;
}
Return $ range;
}
/**
* Recursive output array
*
* @ Param array $ array to be output by arr
* @ Return int returns the number of array elements */
Function recursionarray ($ arr ){
$ I = 0;
Foreach ($ arr as $ value ){
If (is_array ($ value )){
$ I + = recursionarray ($ value );
} Else {
Echo $ value ."
";
$ I ++;
}
}
Return $ I;
}
$ A = array ('A', 'B', 'C', 'D ');
$ B = array ();
$ Range = wholerange ($ a, $ B, $ );
$ Count = recursionarray ($ range );
Echo "a total of". $ count. "arrays ";
?> |
A condition is an arrangement of m (mn) elements from n different elements in a certain order. it is called an arrangement of m elements from n different elements. When...