class HalfFind
{
/**
* @desc 二分法查找 效率老高了 前提: 必须是有序的数组
* @desc 二分法时间复杂度为 O(log n)
*
* @param $nums
* @param $val
* @return float|int
*/
function find($nums, $val)
{
if (count($nums) < 1) {
return $nums;
}
$low = 0;
$high = count($nums) - 1;
// 易错点一: $low <= $high, 而不是<
while($low <= $high) {
// 易错点二 : 中间位置
// 这种写法而不是$key = ($low + $high) / 2 是为了避免当low 和 high都很大的时候求和发生溢出
// 比这种写法效率更高的写法是 : $key = ($low + ($high - $low) >> 1) Why? 因为计算机进行位运算比进行除法运算更快
$key = floor($low + ($high - $low) / 2);
if ($nums[$key] == $val) {
return $key;
}elseif ($nums[$key] < $val) {
// 易错点三: 边界值变更是 key + 1 或是 key - 1
// 🌰 : 当$low = $high = 3, but
【PHP】二分法
最新推荐文章于 2024-08-03 18:54:56 发布