// 递归版本
function bin_sch($arr,$low,$high,$val) { if($low<$high){ $mid = intval(($low+$high)/2); if($val == $arr[$mid]){ return $mid; }else if($val < $arr[$mid]){ return bin_sch($arr,$low,$mid-1,$val); }else{ return bin_sch($arr,$mid+1,$high,$val); } } return -1; }
// 非递归版本
function bin_sch($arr,$val) { $num = count($arr); $low = 0; $high = $c - 1; while($low <= $high){ $mid = intval(($low + $high)/2); if($val < $arr[$mid]){ $mid++; }else if($val > $arr[mid]){ $high--; }else{ return $mid; } } return -1; }