function half($arr,$val){
$end=$num=count($arr)-1;
$start=0;
if(!in_array($val,$arr)){
return false;
}
for($i=0;$i<=$num/2;$i++){
$midle=floor(($start+$end)/2);
echo $arr[$midle].',';
if($arr[$midle]==$val){
return $i.':'.$arr[$midle];
}else{
if($arr[$midle]<$val){
$start=$midle;
//$end=$end;
continue;
}else{
$end=$midle;
//$start=$start;
}
}
}
return false;
}
$arr=array(1,2,3,4,5,8,9,33,55,56,66,88,99);
var_dump(half($arr,33));
阅读(1729) | 评论(0) | 转发(0) |