各种算法复杂度
二分查找
<?php
$arr = [1,10,20,30,255,388];
$search = 255;
$index = bsearch($arr,$search);
echo $index==-1?"找不到":"找到了,位置是".$index;
function bsearch($arr,$search){
$len = count($arr);
$low = 0;
$hei = $len-1;
$flag = -1;
while($low<=$hei){
$index = floor(($hei+$low)/2);
if($search>$arr[$index]){
$low = $index+1;
}elseif($search<$arr[$index]){
$hei = $index-1;
}else{
$flag = $index;
break;
}
}
return $flag;
}
快速查找
<?php
$arr = ['hello','apple','wall','green','orgin'];
echo intval("adfds");exit;
class quiksort{
public $arr;
public function swap(&$a,&$b){
$t = $a;
$a = $b;
$b = $t;
}
public function qsort(&$arr,$left=0,$right=0){
if($right==0){
$right = count($arr)-1;
}
if($left>=$right){
return;
}
$m = $arr[$left];
$i = $left+1;
$j = $right;
for(;;){
while($i<=$right && $arr[$i]<=$m){
$i++;
}
while($j>=0 && $arr[$j]>=$m){
$j--;
}
if($i>=$j){
break;
}
swap($arr[$j],$arr[$i]);
}
swap($arr[$j],$arr[$left]);
qsort($arr,$left,$j-1);
qsort($arr,$j+1,$right);
}
}