Using PHP to implement binary search algorithm code sharing _php Tutorial

Source: Internet
Author: User
The first method:
"Two-point lookup Requirements": 1. Sequential storage structure must be used 2. Must be ordered by keyword size.
"Pros and cons" The advantage of binary lookup method is that the number of comparisons is small, the search speed is fast, the average performance is good, the disadvantage is that the unknown Origin table is ordered and the insertion is difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent.
"Algorithm Idea" first, the table intermediate position record of the keyword compared to the lookup keyword, if the two are equal, the search succeeds; otherwise, the table is divided into the front and the last two sub-tables using the intermediate positional records, and if the middle position record keyword is greater than the lookup keyword, the previous child table is further looked up, otherwise the next child table
Copy CodeThe code is as follows:
Author: The Distant Anticipation
qq:15624575
Home: http://www.phptogether.com/
Array of forward sort
$arr =array (1,3,5,7,9,11);
An array of reverse order
$arr 2=array (11,9,7,5,3,1);
Binary lookup of a forward sorted array
function Searchpart ($arr, $x) {
$start = 0;
$end =count ($arr)-1;
while ($start <= $end) {
$mid =intval (($start + $end)/2);//Here you only need to ensure that the median subscript is calculated as an integer or rounded, without affecting the result
if ($arr [$mid]> $x) {//If the value of the middle item is greater than the unknown origin value, the deviation value is located on the left side of the middle item, so the start subscript is unchanged, the end subscript becomes the middle subscript minus 1, the first search is $arr[0]-$arr [5], the next search
$end = $mid -1;//$arr [0]-$arr [1]
}elseif ($arr [$mid]< $x) {//If the value of the middle item is less than the unknown origin value, the deviation value is at the right of the middle item, so the subscript is not changed, the starting subscript becomes the middle subscript plus 1, the first search is $arr[0]-$arr [5], the next// Second search yes, $arr [3]-$arr [5]
$start = $mid +1;
}else{//found, return unknown origin value subscript
return $mid;
}
}
}
Binary lookup of an array in reverse order
function Searchpart2 ($arr, $x) {
$start = 0;
$end =count ($arr)-1;
while ($start <= $end) {
$mid =intval (($start + $end)/2);//Here you only need to ensure that the median subscript is calculated as an integer or rounded, without affecting the result
if ($arr [$mid]> $x) {//If the value of the middle item is greater than the unknown origin value, the deviation value is at the right of the middle item, so the subscript is not changed, the starting subscript becomes the middle subscript plus 1, the first search is $arr[0]-$arr [5], the next search
$start = $mid +1;//$arr [3]-$arr [5]
}elseif ($arr [$mid]< $x) {//If the value of the middle item is less than the unknown origin value, the difference value is located on the left side of the middle item, so the start subscript is unchanged, the end subscript becomes the middle subscript minus 1, the first search is $arr[0]-$arr [5], the next// Second search yes, $arr [0]-$arr [1]
$end = $mid-1;
}else{//found, return unknown origin value subscript
return $mid;
}
}
}
Echo Searchpart2 ($arr, 5). '
';
Echo searchpart2 ($arr 2,5);
?>

implementation of PHP binary search algorithm
Recently collated the previous learning algorithm knowledge, although in the Web development algorithm use less, but still some useful algorithms to do the backup.
The binary lookup method, also known as the binary lookup method, takes advantage of the order relationship between elements and uses the divide-and-conquer strategy to complete the search task in the worst case with O (log n).
"Basic Ideas"
Divide n elements into roughly the same number of halves, take A[N/2] and the X to find, if X=A[N/2] Find x, the algorithm terminates. If XA[N/2], then we just continue to search for x in the right half of array A.
The binary search method is very widely used, and its ideas are easy to understand. The first binary search algorithm appeared as early as 1946, but the first completely correct binary search algorithm didn't appear until 1962. Bentley wrote in his book, Writing Correct Programs, that 90% of computer experts could not write exactly the right binary search algorithm within 2 hours. The crux of the problem is to accurately define the boundaries of the search scope and the determination of the termination conditions, correctly summed up the odd even of the various situations, in fact, after finishing can find its specific algorithm is very intuitive.
Implementation of PHP binary search algorithm
Copy CodeThe code is as follows:
/**
* Binary search algorithm
*
* @param array $arr ordered
* @param int $val the value to find
* @return int lookup value exists return array subscript, no return-1
*/
function Binary_search ($arr, $val)
{
$l = count ($arr);//get ordered array length
$low = 0;
$high = $l-1;
while ($low <= $high)
{
$middle = Floor (($low + $high)/2);
if ($arr [$middle] = = $val)
{
return $middle;
}
ElseIf ($arr [$middle] > $val)
{
$high = $middle-1;
}
Else
{
$low = $middle + 1;
}
}
return-1;
}
Example
$arr = Array (1,2,3,4,5,6,7,8,9,12,23,33,35,56,67,89,99);
Echo Binary_search ($arr, 57);

http://www.bkjia.com/PHPjc/323637.html www.bkjia.com true http://www.bkjia.com/PHPjc/323637.html techarticle The first method: "Two-point lookup requirement": 1. Sequential storage structure must be used 2. Must be sorted by keyword size. "Advantages and disadvantages" Binary lookup method is the advantage of less than the number of comparisons, check ...

  • 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.