Php array binary search function code. Copy the code as follows :? In the phpsearch 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 functionsearch (
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 http://www.bkjia.com/PHPjc/321261.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321261.htmlTechArticle code is as follows :? In the php // search function, $ array is the array, $ k is the value to be searched, and $ low is the minimum key value in the search range, $ high is the maximum key value of the search range function search (...