时间:2021-07-01 10:21:17 帮助过:5人阅读
$Arr = array(1,3,4,5,7,8,9);7,0,6
Search($Arr,7,0,count($Arr)-1);
function Search($Arr,$FindVal,$LeftIndex,$RightIndex){
echo "$FindVal,$LeftIndex,$RightIndex\n"; //加上这句
if($FindVal>$Arr[count($Arr)-1]){
echo "找不到该值";
}else if($FindVal<$Arr[0]){
echo "找不到该值";
}else{
$MiddleIndex=round(($LeftIndex+$RightIndex)/2);
if ($Arr[$MiddleIndex]<$FindVal){
Search($Arr,$FindVal,++$MiddleIndex,$RightIndex);
}else if($Arr[$MiddleIndex]>$FindVal){
Search($Arr,$FindVal,$LeftIndex,--$MiddleIndex);
}else{
echo "找到下标为$MiddleIndex";
}
}
}