按數量級遞增排列
//二分查找O(log
function erfen($a
if($l >$h){ return false;}
$m = intval(($l+$h)/
if ($a[$m] == $f){
return $m;
}elseif ($f < $a[$m]){
return erfen($a
}else{
return erfen($a
}
}
$a = array(
var_dump(erfen($a
//遍歷樹O(log
function bianli($p){
$a = array();
foreach (glob($p
if(is_dir($f)){
$a = array_merge($a
}else{
$a[] = $f;
}
}
return $a;
}
//階乘O(log
function jc($n){
if($n<=
return
}else{
return $n*jc($n
}
}
//快速查找 O(n *log
function kuaisu($a){
$c = count($a);
if($c <=
$l = $r = array();
for ($i=
if($a[$i] < $a[
$l[] = $a[$i];
}else{
$r[] = $a[$i];
}
}
$l = kuaisu($l);
$r = kuaisu($r);
return array_merge($l
}
//插入排序 O(N*N)
function charu($a){
$c = count($a);
for($i=
$t = $a[$i];
for($j=$i;$j>
$a[$j] = $a[$j
}
$a[$j] = $t;
}
return $a;
}
//選擇排序O(N*N)
function xuanze($a){
$c = count($a);
for($i=
for ($j=$i+
if($a[$i]>$a[$j]){
$t = $a[$j];
$a[$j] = $a[$i];
$a[$i] = $t;
}
}
}
return $a;
}
//冒泡排序 O(N*N)
function maopao($a){
$c = count($a);
for($i=
for ($j=$c
if($a[$j] < $a[$j
$t = $a[$j
$a[$j
$a[$j] = $t;
}
}
}
return $a;
} 復制代碼 代碼如下:
/**
* 排列組合
* 采用二進制方法進行組合的選擇
*
* @param 需要排列的數組 $arr
* @param 最小個數 $min_size
* @return 滿足條件的新數組組合
*/
function plzh($arr
$len = count($arr);
$max = pow(
$min = pow(
$r_arr = array();
for ($i=$min; $i<$max; $i++){
$count =
$t_arr = array();
for ($j=
$a = pow(
$t = $i&$a;
if($t == $a){
$t_arr[] = $arr[$j];
$count++;
}
}
if($count == $size){
$r_arr[] = $t_arr;
}
}
return $r_arr;
}
$pl = pl(array(
var_dump($pl);
From:http://tw.wingwit.com/Article/program/PHP/201311/21271.html