PHP Find binary search

Source: Internet
Author: User

Binary lookup, often for an ordered array of lookups, we assume that a sequence is an array ordered, and then given a number, to find out where it should be sorted in this array

The Baidu Encyclopedia

Binary lookup is also known as binary lookup (binary search), which is a highly efficient method of finding. However, the binary lookup requires that the linear table must take a sequential storage structure, and that the elements in the table are sorted by keyword

Binary look for the name and you'll know what's going on.

Let's give an example.

1 3 5 7 9 is an already sequenced array, and if you ask for X now, how do we find out where x is placed?

Obviously, we can take the most middle one, and if not, see if the middle one is greater than X or less than x if it is less than x, that x should be in the second half, if it is greater than X, then in the first half, then the current middle node as the starting or terminating node to form a new array, again, so return , you can always find the location of X.

Or just look at the code, the simplest way, recursive implementation, the code is as follows:

<?PHPfunctionBinary_search ($array,$target,$start,$end){    $middle _index= Floor(($start+$end)/2);//Locate the index of the middle position    $middle _value=$array[$middle _index];//gets the value of the middle item    if($middle _value==$target)    {        return $middle _index; }Else if($middle _value>$target)    {        if($start>$middle _index-1)//if the starting position is larger than the end position, it means that it must not be found.        {            return false; }        //the middle is bigger than the target you're looking for, go to the left.        $re= Binary_search ($array,$target,$start,$middle _index-1); }Else    {        if($middle _index+1 >$end)//if the starting position is larger than the end position, it must not be found.        {            return false; }        //the middle item is smaller than the target to find it on the right .        $re= Binary_search ($array,$target,$middle _index+1,$end); }    return $re;}$array=Array(1,3,5,7,9,10);$search= 9;//the number to find$index= Binary_search ($array,$search, 0,Count($array)-1);Var_dump($index);

As follows:

We can see that the use of recursive understanding is very simple, will be punches, trivial, so reciprocating to solve, segmented sub-block, gradually find the element to be changed, if not found to return false.

So, since recursion has always been considered inefficient, how do you use other methods to rewrite the search method?

We can use the while loop to implement the iterative way, the code is as follows:

<?PHPfunctionBinary_search ($array,$target,$start,$end){    $middle _index= Floor(($start+$end)/2);  while($start<=$end)    {        //The explanation will continue to find        if($array[$middle _index]==$target)        {            //found the            return $middle _index; }ElseIf($array[$middle _index]>$target)        {            //indicates that the intermediate element is greater than the target element in the first half            $end=$middle _index-1;//The terminating position ends at the left position of the middle position you just found.            $middle _index= Floor(($start+$end)/2);//Recalculate Intermediate positions}Else        {            //indicates that the intermediate element is less than the target element in the first half            $start=$middle _index+1;//start position in the middle position just found in the right position.            $middle _index= Floor(($start+$end)/2);//Recalculate Intermediate positions        }    }    return false;//return false not found}$array=Array(1,3,5,7,9,10);$search= 9;//the number to find$index= Binary_search ($array,$search, 0,Count($array)-1);Var_dump($index);

As follows:

In this case, the number of cycles is also less, rewriting the recursion, reducing the complexity of the calculation.

PHP Find binary search

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.