This article is a PHP in the common algorithm and time complexity of the detailed analysis of the introduction, the need for friends to refer to the
By order of magnitude, the common time complexity is: Constant order O (1), Logarithmic order O (log2n), linear order O (n), linear logarithmic order O (nlog2n), square O (n2), Cubic O (N3)
Copy Code code as follows:
//Two point search O (log2n)
function Erfen ($a, $l, $h, $f) {
if ($l > $h) {return false;}
$m = Intval (($l + $h)/2);
if ($a [$m] = = $f) {
return $m;
}elseif ($f < $a [$m]) {
return Erfen ($a, $l, $m-1, $f);
}else{
return Erfen ($a, $m +1, $h, $f);
}
}
$a = array (1,12,23,67,88,100);
Var_dump (Erfen ($a, 0,5,1));
//Traverse tree O (log2n)
function Bianli ($p) {
$a = array ();
foreach (Glob ($p. '/* ') as $f) {
if (Is_dir ($f)) {
$a = array_merge ($a, Bianli ($f));
}else{
$a [] = $f;
}
}
return $a;
}
//factorial O (log2n)
function JC ($n) {
if ($n <=1) {
return 1;
}else{
return $n *JC ($n-1);
}
}
//Quick Find O (n *log2 (n))
function Kuaisu ($a) {
$c = count ($a);
if ($c <= 1) {return $a;}
$l = $r = Array ();
for ($i =1; $i < $c; $i + +) {
if ($a [$i] < $a [0]) {
$l [] = $a [$i];
}else{
$r [] = $a [$i];
}
}
$l = Kuaisu ($l);
$r = Kuaisu ($r);
return Array_merge ($l, array ($a [0]), $r);
}
//Insert sort O (n*n)
function Charu ($a) {
$c = count ($a);
for ($i =1; $i < $c; $i + +) {
$t = $a [$i];
for ($j = $i; $j >0 && $a [$j -1]> $t; $j-) {
$a [$j] = $a [$j-1];
}
$a [$j] = $t;
}
return $a;
}
//select sort O (n*n)
function Xuanze ($a) {
$c = count ($a);
for ($i =0; $i < $c; $i + +) {
for ($j = $i +1; $j < $c; $j + +) {
if ($a [$i]> $a [$j]) {
$t = $a [$j];
$a [$j] = $a [$i];
$a [$i] = $t;
}
}
}
return $a;
}
//Bubble sort O (n*n)
function Maopao ($a) {
$c = count ($a);
for ($i =0; $i < $c; $i + +) {
for ($j = $c-1; $j > $i; $j-) {
if ($a [$j] < $a [$j-1]) {
$t = $a [$j-1];
$a [$j-1] = $a [$j];
$a [$j] = $t;
}
}
}
return $a;
}
Copy Code code as follows:
/**
* Arrangement Combination
* Using the binary method of the combination of choice, such as the 5 selection of 3 o'clock, only 3 bits to 1 on it, so the combination can be 01101 11100 00111 10011 01110, such as 10 of the combination
*
* @param the array to be arranged $arr
* @param minimum number $min _size
* @return new array combinations that meet the criteria
*/
function Plzh ($arr, $size =5) {
$len = count ($arr);
$max = POW (2, $len);
$min = POW (2, $size)-1;
$r _arr = Array ();
for ($i = $min; $i < $max; $i + +) {
$count = 0;
$t _arr = Array ();
for ($j =0; $j < $len; $j + +) {
$a = POW (2, $j);
$t = $i & $a;
if ($t = = $a) {
$t _arr[] = $arr [$j];
$count + +;
}
}
if ($count = = $size) {
$r _arr[] = $t _arr;
}
}
return $r _arr;
}
$PL = PL (Array (1,2,3,4,5,6,7), 5);
Var_dump ($PL);