How PHP implements the binary lookup algorithm

Source: Internet
Author: User
This paper mainly introduces the implementation of PHP binary search algorithm, a simple description of the principle of binary lookup, and combined with the example of PHP to implement the recursive and non-recursive way to achieve binary search algorithm related operation skills, the need for friends can refer to, hope to help everyone.

definition: binary Find technology, that is, binary search. The premise is that the records in a linear table must be order-critical (usually from large to small), and linear tables must be stored sequentially.

binary Find the basic idea: take the intermediate record as the comparison object, if the given value and the middle record of the keyword, then in the middle record the key word is equal, then the search succeeds; If the given value is less than the intermediate record of the companion to continue to find, if the given value is greater than the middle record of the keyword, Continue searching in the right half of the middle record. Repeat the process until the lookup succeeds, or if all lookup zones are not logged, the lookup fails.

Implementation code:


<?php//recursive mode function Bin_recur_search ($arr, $val) {global $time;    if (count ($arr) >= 1) {$mid = Intval (count ($arr)/2);    $time + +; The IF ($arr [$mid] = = $val) {return ' value is: '. $arr [$mid]. ' <br> lookups: '. $time. '    <br> ';      }elseif ($arr [$mid] > $val) {$arr = Array_splice ($arr, 0, $mid);    Return Bin_recur_search ($arr, $val);      }else{$arr = Array_slice ($arr, $mid + 1);    Return Bin_recur_search ($arr, $val); }} return ' not found '. $val;}    Non-recursive function bin_search ($arr, $val) {if (count ($arr) >= 1) {$low = 0;    $high = count ($arr);    $time = 0;      while ($low <= $high) {$time + +;      $mid = Intval (($low + $high)/2); if ($val = = $arr [$mid]) {return ' index: '. $mid. ' <br> value: '. $arr [$mid]. '      <br> number of lookups: '. $time;      }elseif ($val > $arr [$mid]) {$low = $mid + 1;      }else{$high = $mid-1; }}} return ' not found '. $val;} $arr = Array (1,3,5,7,7,9,25,68,98,145,673,8542); Echo Bin_recur_search ($arr, 673); Echo Bin_search ($arr, 673);? >

Operation Result:

Values are: 673 lookup times: 4 Index: 10 value is: 673 number of Lookups: 4

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.