Binary lookup may be used in the development of advanced points. Of course, this is the case when you are looking for a job in a large company. Let's take a look at the implementation method of binary lookup in php, the details are as follows. Binary lookup may be used in the development of advanced points. Of course, this is the case when you are looking for a job in a large company. Let's take a look at the implementation method of binary lookup in php, the details are as follows.
Script ec (2); script
Dichotomie is the method of dividing two parts. set [a, B] to a closed range of R. the successive division is to create the following interval sequence ([an, bn]): a0 = a, b0 = B, and for any natural number n, [an + 1, bn + 1] or [an, cn], or [cn, bn]. cn indicates the midpoint of [an, bn.
Example 1
Header ('content-Type: text/html; charset = UTF-8 ;');
$ Arr = array (323,321, 90,123 );
Sort ($ arr );
// Binary Search
Echo $ index = binarySearch ($ arr, 321 );
Function binarySearch ($ arr, $ key ){
$ Len = count ($ arr );
$ Mid =-1;
$ Start = 0;
$ End = $ len-1;
While ($ start <= $ end ){
$ Mid = (int) ($ start + $ end)/2 );
Echo $ mid. "\ n ";
If ($ arr [$ mid] ==$ key ){
Return $ mid;
} Else if ($ arr [$ mid] <$ key ){
$ Start = $ mid + 1;
} Else if ($ arr [$ mid]> $ key ){
$ End = $ mid-1;
}
}
}
Example 2
// 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.
?>