Using PHP to implement a binary lookup algorithm code-sharing

Source: Internet
Author: User
Tags array array length continue count implement integer advantage

The first method:
"Two-point search Requirements": 1. Sequential storage structure must be used 2. Ordered by keyword size.
"Advantages and Disadvantages" Binary search method has the advantage of less number of comparisons, faster search speed, good average performance; its disadvantage is that the table of discovery is ordered, and the insertion deletion is difficult. Therefore, the binary lookup method is suitable for frequently ordered lists that are infrequently changed.
"Algorithmic thinking" first, compare the keywords in the middle position of the table with the Lookup keyword, if the two are equal, the search succeeds, otherwise the table is divided into the first and last two child tables using the intermediate position record, if the key of the middle position record is greater than the lookup keyword, the previous child table is further searched, otherwise the next child table is further searched.
Copy CodeThe code is as follows:
<?php
Author: The Distant Anticipation
qq:15624575
Home: http://www.phptogether.com/
Array of forward sort
$arr =array (1,3,5,7,9,11);
Arrays in 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 make sure that the middle item subscript evaluates to an integer, or rounded, without affecting the result
if ($arr [$mid]> $x) {//If the value of the intermediate item is greater than the value of the estimate, description the difference is located in the middle of the left side, so the starting subscript unchanged, the end of the subscript into the middle of the 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 intermediate item is less than the value of the estimate, the value of the difference is located on the right side of the middle item, so the ending subscript is unchanged, the starting subscript becomes the middle item subscript plus 1, and the first search is $arr[0]-$arr [5]. Second search is, $arr [3]-$arr [5]
$start = $mid +1;
}else{//found it, return the value of the index subscript
return $mid;
}
}
}
Binary lookup of a reverse-ordered array
function Searchpart2 ($arr, $x) {
$start = 0;
$end =count ($arr)-1;
while ($start <= $end) {
$mid =intval (($start + $end)/2)//Here you only need to make sure that the middle item subscript evaluates to an integer, or rounded, without affecting the result
if ($arr [$mid]> $x) {//If the value of the intermediate item is greater than the value of the estimate, description the difference is located on the right side of the middle item, therefore, the end of the subscript unchanged, the starting subscript into the middle of the 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 intermediate item is less than the value of the estimate, the value of the difference is located on the left side of the middle item, so the starting subscript is unchanged, the end subscript becomes the subscript minus 1, and the first search is $arr[0]-$arr [5]. Second search is, $arr [0]-$arr [1]
$end = $mid-1;
}else{//found it, return the value of the index subscript
return $mid;
}
}
}
Echo Searchpart2 ($arr, 5). ' <br> ';
Echo searchpart2 ($arr 2,5);
?>

implementation of PHP binary lookup algorithm
Recently sorted out the previous learning algorithm knowledge, although the Web development in the algorithm used less, but still some useful algorithms to do the backup.
The binary lookup method, also known as the binary lookup method, takes full advantage of the order relations among elements and uses the partition strategy to complete the search task with O (log n) in the worst case.
"Basic Ideas"
n elements are divided into roughly the same number of two halves, take A[N/2] compared to the X to look for, if X=A[N/2] Find x, the algorithm terminates. If X&LT;A[N/2], we just continue searching for X in the left half of the array a (assuming that the array elements are in ascending order). If X&GT;A[N/2], we just continue searching for X in the right half of the array A.
The application of the binary search method is very extensive, and its idea is easy to understand. The first binary search algorithm appeared as early as 1946, but the first fully correct binary search algorithm did not appear until 1962. In his book, "Writing Correct Programs," Bentley wrote that 90% of the computer experts could not write a completely correct binary search algorithm within 2 hours. The crux of the problem lies in the accurate formulation of the boundary of the search scope and the determination of the termination condition, and the correct induction of the odd and even conditions, in fact, it can be found that the specific algorithm is very intuitive.
Implementation of PHP binary lookup algorithm
Copy CodeThe code is as follows:
/**
* Two-point search algorithm
*
* $arr ordered array @param array
* @param the value of the int $val lookup
* @return int Lookup value exists returns an array subscript, does not exist 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);



Related Article

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.