首页 > 代码库 > php实现二分查找

php实现二分查找

<?phpclass BinSch{	/*	 * 二分查找,针对顺序表才有用	 */	public function search($arr,$k,$low,$high){		if($low<=$high){			$mid = floor(($low+$high)/2);		}		if ($arr[$mid] == $k){			return $mid;		}				if($arr[$mid]>$k){			return $this->search($arr, $k, $low, $mid-1);		}				if ($arr[$mid]<$k){			return $this->search($arr, $k, $mid+1, $high);		}				return false;	}}class Client{	public static function main(){		$arr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);		$obj = new BinSch();		echo $obj->search($arr, 10, 0, 15);	}}Client::main();?>

  

php实现二分查找