![图片上传中...]
找100个数字中的其中一个 按道理说 应该最多7步才对 怎么就15步了呢
小伙看你根骨奇佳,潜力无限,来学PHP伐。
The first execution of $flag = 50 uses else logic, but $i++ and $low+1 are not assigned to $low. Therefore, keep looping else
Modify line 24 $low= $low+1;
function binarySearch($array, $val) { $count = count($array); $low = 0; $high = $count - 1; while ($low <= $high) { $mid = intval(($low + $high) / 2); if ($array[$mid] == $val) { return $mid; } if ($array[$mid] < $val) { $low = $mid + 1; } else { $high = $mid - 1; } } return false; }
The first execution of $flag = 50 uses else logic, but $i++ and $low+1 are not assigned to $low. Therefore, keep looping else
Modify line 24 $low= $low+1;