代码如下
$arr = [1,3,5,7,9];//$arr = range(1,10000);var_dump(find($arr, 2));
function find(array $arr, $seach) { $left = 0; $right = count($arr) - 1; if($seach > $arr[$right]) { return $arr[0]; } while($left < $right) { $mod = (($left + $right +1) >> 1); echo $mod,‘<br >‘; if($mod == $right) { return $arr[$mod]; } if($arr[$mod] < $seach) { $left = $mod; } else if ($arr[$mod] > $seach) { $right = $mod; } else { return $arr[$mod]; } } return false; }
时间: 2024-12-08 00:33:22