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.