<php function binarySearch($arr,$a){ $low = 0; $high = count($arr)-1; $mid = ceil(($low+$high)/2);while($low<=$high){ if($a==$arr[$mid]){ echo "查找的数的位置是第"."$mid"; } if($a>$arr[$mid]){ $high = count($arr)-1; $low = $mid+1; $mid = ceil(($low+$high)/2); } if ($a<$arr[$mid]) { $low = 0; $high = $mid-1; $mid = ceil(($low+$high)/2); } }}$arr1 = array(5,7,9,10,12,16,19);binarySearch($arr1,12);?>
function binarySearch($arr,$a){
$mid = ceil( ($low+$high)/2);
$n = 0;無限ループ
while($low<=$high && $n++
$high = カウント ($ Arr) - 1;
$ Low = $ MID -1; // ここで注意してください
$ MID = CEIL (($ Low+$ High)/2); {
$low = $mid
}
を押しますphpは実際にできる書かれてください
function binarySearch($arr,$a){ $low = 0; $high = count($arr)-1; $mid = ceil(($low+$high)/2); $num = count($arr); while($low<=$high && $num--){ if($a==$arr[$mid]){ echo "查找的数的位置是第"."$mid"; break; } list($low, $high) = $a > $arr[$mid] ? array($mid, $high) : array($low, $mid); $mid = ceil(($low+$high)/2); }}