二分查找完美版
/** * 有序数组为升序 */ function bsearch($start, $end, $des, &$arr) { $mid = round(($start+$end)/2); if ($arr[$start] > $des or $arr[$end] < $des) { return FALSE; } else if (($end - $start) == 1) { return ($arr[$start] == $des or $arr[$end] == $des); } if ($arr[$mid] > $des) { return bsearch($start, $mid, $des, $arr); } else if ($arr[$mid] == $des) { return TRUE; } else if ($arr[$mid] < $des) { return bsearch($mid, $end, $des, $arr); } } $arr = array(1,2,3,4,5,9,10,11,54,87); $ret = bsearch(0, count($arr)-1, 8, $arr); var_dump($ret);