function binarySearch($arr,$findval,$leftIndex,$rigthIndex){ if($leftIndex > $rigthIndex){ echo ‘找不到这个数‘; return ; } $middleIndex = round(($leftIndex+$rigthIndex)/2); if($findval > $arr[$middleIndex]){ binarySearch($arr, $findval, $middleIndex+1, $rigthIndex); }else if($findval < $arr[$middleIndex]){ binarySearch($arr, $findval, $leftIndex, $middleIndex-1); }else{ echo ‘这个数的下标是‘. $middleIndex; } }
时间: 2024-10-17 19:08:08