<?php
function binsearch($n,$array){
$start = 0;
$end = count($array);
$pos = false;
do{
$mid = (int) (($start + $end)/2);
if($n > $array[$mid]){
$start = $mid;
}else if($n < $array[$mid]){
$end = $mid;
}else if($n===$array[$mid]){
return $mid;
}
if($start===$mid || $end===$mid){
return false;
}
}while(!$pos);
return false;
}
$n = 6;
$array=[1,2,4,5,7,8,9,10];
echo binsearch($n,$array);
查找算法-二分查找
于 2018-04-11 00:23:44 首次发布