Php code for Binary Search-PHP source code

Source: Internet
Author: User
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.
?>

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.