<?php
public function select_two_part($a,$n,$target){
$l = 0;
$r = $n-1;
$mid = 0;
while($l<=$r){
$mid = round(l+r)/2;
//round(l+(r-l)/2);这个可以避免Int型越界,在强类型语言中会遇到
if($target = $arr[$mid])
{ return $mid; }
if($target>$mid){
$l = $mid+1;
}else{
$r = $mid-1;
}
}
return -1;//没有找到返回-1
}
php实现二分查找法
最新推荐文章于 2024-03-08 09:18:57 发布