PHP Algorithm for binary lookup

Source: Internet
Author: User
Find the query speed of the database when you see two points to find, and then recorded, a do not understand, after reading too simple algorithm.
Let's talk about the concept of binary lookup:
Binary search, also known as binary lookup, binary search is also called binary search, the advantage is that the comparison of less times, the faster the search speed, the average performance is good; its disadvantage is that the table is ordered table, and insert delete difficult. Therefore, the binary lookup method is suitable for frequently ordered lists that are infrequently changed. First, suppose that the elements in the table are sorted in ascending order, compares the keywords in the middle position record of the table to the lookup key, and if the two are equal, the search succeeds; otherwise, the table is divided into the preceding and the last two child tables using the middle position record, and the previous child table is further searched if the key of the middle position record is greater than the lookup keyword Otherwise, the next child table is further searched. Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful. "Baidu Encyclopedia"


Binary lookup, as the name implies is half of the search, first find the middle item, the middle item is equal to look for the direct return, if less than, then in the current intermediate to the maximum value of the middle of the comparison, if greater than, in the minimum to intermediate items to find the middle of the comparison, circular lookup.

First of all to know that the operation of the binary search is an ordered table , is a good order, such as to find a 1 million data in a desired element, the average query will be checked 500,000 times, and the two points to find the maximum need to check 20 times, Read the principle to know why the binary search how may need so few times.

/** * @param $arr ordered table to be queried (ordered array) * @param $needle The value to be queried * @return The key of the successful return value is not successfully returned-1 * * $low array k  
    EY Lookup Range Minimum * $top array Key's lookup range maximum */function binary ($arr, $needle) {$low = 0; 
    $top = count ($arr); 
            while ($low <= $top) {//Loop lookup $mid = Floor (($low + $top)/2)///down///binary array, and compare array intermediate values to values to query 
            if ($arr [$mid]== $needle) {//If the median value is equal to the value to be queried, returns the key return $mid of the current intermediate value directly; 
            }elseif ($arr [$mid]< $needle) {//If the median value is less than the value to be queried, then the key plus 1 of the middle value is treated as the minimum value of the query range, participating in the next cycle $low = $mid +1; 
            }else{//If the median value is greater than the value to be queried, then the key minus 1 of the intermediate value is treated as the maximum of the query range, participating in the next cycle $top = $mid-1; 
return-1;//Find unsuccessful} $arr = Array (1,2,3,4,5,9,23,44,55,56,77,89); 
echo Binary ($arr, 44); /* $arr The result of Count () is $top=12, the value of the key is 12/2 = 6, the first loop according to code to compare $arr[6] and, $arr [6] = 23;23<44, so the middle value of key plus 1 as the minimum value of the query range, At this time the $low=7, $top = 12; At this point the median becomes (7+12)/2 = 9 (9.5 of the downward rounding) continues the second loop to execute the code, comparing $arr[9] and the $arr [6] = 56;56>44, so the median key minus 1 is the maximum of the query range, at which point the $low = 7, $top =8 (9-1), at which point the median becomes (7+8)/2 = 7, and continues the third loop to execute the code, comparing $arr[7] and the $arr [7] = 44; Then the key that returns the current median is: 7.  */


The above is a simple binary lookup, see one time cycle, and each loop is the three logic, here will think of recursion:


/** 
 * @param $arr  the ordered array to find 
 @param $low  minimum * @param 
 $top  maximum * @param $target  the element to query 
 * @return Float|int  Returns the result * 
/function recursion ($arr, $low, $top, $target) { 
    if ($low <= $top) { 
        $mid = Floor (($low + $top)/2); 
        if ($arr [$mid]== $target) {return 
            $mid; 
        } ElseIf ($arr [$mid]< $target) {return 
            recursion ($arr, $mid +1, $top, $target); 
        else{return 
            recursion ($arr, $low, $mid-1, $target); 
        } 
    else{ 
        return-1 
    } 
} 
$arr = Array (1,2,3,4,5,9,23,44,55,56,77,89); 
echo recursion ($arr, 0,count ($arr), 44); 


Original from: http://www.yigangwu.com/index.php?m=content&c=index&a=show&catid=28&id=35 Click on Open link



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.