Home > Backend Development > PHP Tutorial > 递归二分查找 望大姐 帮忙 求解释

递归二分查找 望大姐 帮忙 求解释

WBOY
Release: 2016-06-13 12:47:15
Original
923 people have browsed it

递归二分查找 望各位大哥大姐 帮忙 求解释
$Arr=array(1,2,3,4,5,6);
Search($Arr,6,0,count($Arr)-1);
function Search($Arr,$FindVal,$LeftIndex,$RightIndex){
if($FindVal>$Arr[count($Arr)-1]){
echo "找不到该值";
}else if($FindVal echo "找不到该值";
}else{
$MiddleIndex=round(($LeftIndex+$RightIndex)/2);
if ($Arr[$MiddleIndex] Search($Arr,$FindVal,++$MiddleIndex,$RightIndex);
}else if($Arr[$MiddleIndex]>$FindVal){
Search($Arr,$FindVal,$LeftIndex,--$MiddleIndex);
}else{
echo "找到下标为$MiddleIndex";
}
}
}
?> 这是递归的二分查找的代码 求高手细致深入解释 目前对这个递归的方法 不知道是怎么实现的 望深入解释 尤其是每次判断符合条件时候 然后再次调用函数 我就有点晕了 谢谢大家帮忙解释

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template