In the search function, $ array is the array, $ k is the value to be searched, $ low is the minimum key value in the search range, and $ high is the maximum key value in the search range.
The code is as follows:
// In the search function, $ array is the array, $ k is the value to be searched, $ low is the minimum key value of the search range, and $ high is the maximum key value of the search range.
Function search ($ array, $ k, $ low = 0, $ high = 0)
{
If (count ($ array )! = 0 and $ high = 0) // determines whether it is the first call.
{
$ High = count ($ array );
}
If ($ low <= $ high) // if there are still remaining array elements
{
$ Mid = intval ($ low + $ high)/2); // Obtain the center values of $ low and $ high.
If ($ array [$ mid] === k) // if it is found, return
{
Return $ mid;
}
Elseif ($ k <$ array [$ mid]) // if not found, continue searching
{
Return search ($ array, $ k, $ low, $ mid-1 );
}
Else
{
Return search ($ array, $ k, $ mid + 1, $ high );
}
}
Return-1;
}
$ Array = array (,); // test the search function
Echo search ($ array, 8); // call the search function and output the search result.
?>
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.