Recursive two-point search elder sister to help beg for explanation

Source: Internet
Author: User
Recursive binary search I hope you Big Brother elder sister help Beg explanation
$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 "Could not find this value";
}else if ($FindVal < $ARR [0]) {
echo "Could not find this value";
}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 "Find subscript for $middleindex";
}
}
}
?> This is a recursive binary lookup code for the master meticulous in-depth interpretation of the current recursive method does not know how to realize the look in-depth explanation especially every time the judgment meets the conditions and then call the function again I'm a little dizzy. Thank you for helping us explain.

Share to:


------Solution--------------------
Teach you how to draw a time series diagram, your own multi-analysis

------Solution--------------------
There is no connection between the internal and external variable names of a function without your code being issued, so you can change the order arbitrarily.
Take the timing diagram above, you can assume that each column is a completely new environment, for example, 3 columns have variable $ A, but they refer to different things (memory space)
$ A in the global environment
------Solution--------------------
$ A In the first function environment
------Solution--------------------
$ A In the second function environment
Although they are all the same in name, they are really different.

It is like the first floor has Zhang San, the second floor has the Zhang San, the third floor has the Zhang San, refers to 3 people, but is not the same person, because they are in the environment is different.

But based on the logic of your code, you can't change it, because $rightindex in Environment 1 and $RIGHTINDEX in Environment 2 need the same value, so you can't change it.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.