PHP binary Search Algorithm example analysis PHP tips

Source: Internet
Author: User
This article mainly introduces PHP binary (binary) search algorithm, combined with the case of the form of a more detailed analysis of the PHP binary (binary) search algorithm concept, principle, implementation and use method, and with a PHP binary (binary) search algorithm class for everyone to reference, the need for friends can refer to the next

The example of PHP binary (binary) lookup algorithm is described in this paper. Share to everyone for your reference, as follows:

The binary query is only applicable to arrays, strings, etc. that have been sorted in a positive or reverse order;

Algorithm:

Takes the middle position of the array first, without the middle position, then takes down the whole;

From the middle of the binary, size judgment, into the first half or the second half of the paragraph;

Make the same binary query for the first half or the second half of the paragraph,

Stop until the matching character is queried (this example uses break, if placed in a function, return)

PHP implements the following code:

<?php$arr = Array (1,2,3,4,5,6,7,8,9,10);//Array $key = 4;//the keyword to query $low = 0;//start bit flag $high = count ($arr);//The flag of the terminating bit while ($  Low <= $high) {//The condition at which the query begins to end $mid = Floor (($low + $high)/2);//Intermediate position calculation, rounding down if ($arr [$mid] = = $key) {//query successful echo $arr [$mid]; break;//end of this page, function can return}elseif ($arr [$mid] > $key) {//Query the first half, move the end flag to the middle of the previous position $high = $mid-1;} else{//The second half of the query, move the start position to the middle position of the latter $low = $mid + 1;}} /* Run result:4*/?>

Add: Binary (binary) find the algorithm class:

/** * description:php The class that implements the binary lookup algorithm * @author wzy */class binary_search{public  $arr;  public $key;  function __construct ($arr, $key) {    //the array initialized here is already an ordered array    $this->arr= $arr;    $this->key= $key;  }  function BinarySearch () {    $start =0;    $end =count ($this->arr)-1;    while ($start <= $end) {      //mid can be either an integer or an integer      $mid =ceil ($start + $end)/2);      $mid = ($start + $end) >>1;      $mid =intval (($start + $end)/2);      if ($this->arr[$mid]< $this->key) {        $start = $mid +1;      } else if ($this->arr[$mid]> $this->key) {        $end = $mid-1;      } else{        return $mid;}}}  

You may also encounter this situation where the elements in the array have duplicate data and need to return the position of the first element in the repeating data, for example

$arr =array (1,2,3,4,5,6,6,6,6,7,8);

The position returned when looking for the 6 element should be 5, not the other (the subscript starts with a 0 count), so it needs to be judged in the returned mid, with the following code:

/** * description:php The class that implements the binary lookup algorithm * @author wzy */class binary_search{public  $arr;  public $key;  function __construct ($arr, $key) {    //the array initialized here is already an ordered array    $this->arr= $arr;    $this->key= $key;  }  function BinarySearch () {    $start =0;    $end =count ($this->arr)-1;    while ($start <= $end) {      //mid can be either an integer or an integer      $mid =ceil ($start + $end)/2);      $mid = ($start + $end) >>1;      $mid =intval (($start + $end)/2);      if ($this->arr[$mid]< $this->key) {        $start = $mid +1;      } else if ($this->arr[$mid]> $this->key) {        $end = $mid-1;      } else{        //Returns the first matching element for        ($i = $mid-1; $i >=0; $i-) {          if ($this->arr[$i]== $this->key) {            $ Mid= $i;          } else{break            ;          }        }        return $mid;}}}  

Articles you may be interested in:

PHP binary (binary) Find algorithm example analysis PHP tips

Layui Framework implements file upload and TP3.2.3 example of background processing of uploaded files

Laravel Framework implementation of the model layer of additions and deletions to change the operation example

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.