PHP binary search

Source: Internet
Author: User
The idea of the PHP binary lookup method is to give me an array like this & nbsp; I add 2 to the header and end indexes of the array to get the index at the intermediate position. take the value of the number I am looking for and compare it with the value at the intermediate index position. if it is equal, this intermediate position is returned.. if the value at the intermediate position is greater than the value you want to search for, use the index at the intermediate position as the final index, and then add 2 to the first index to get the comparison of the intermediate index, if the value at the intermediate position is smaller than the value to be searched, the intermediate PHP binary query is performed.
The idea of the bipartite method is to give me an array. I add the first and last indexes of the array in addition to 2, so that we can get the index in the middle and get the value of the number I am looking for and the intermediate index position. to compare
If they are equal, the index at the intermediate position is returned. if the value at the intermediate position is greater than the value to be searched, the index at the intermediate position is used as the final index, and then the index is added to the header index to get the intermediate index comparison,
If the value at the intermediate position is smaller than the value to be searched, the index at the intermediate position is used as the header index, and then the index is added to the end index to get the comparison of the intermediate index.
 (2*$m)){                                        $m= (int)(($m+$EndPos)/2)+1;                                }                        }else if($arr[$m] > $Num){                                $EndPos = (int)$m;                                 $m= (int)(($m+$StartPos)/2);                        }                }                return $m;        }?>

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.