Using PHP to implement binary search algorithm code sharing

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

<?php//forward sorted array $arr =array (1,3,5,7,9,11); An array of inverse sorts $arr 2=array (11,9,7,5,3,1); Binary lookup for 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 difference is on the left side of the middle item, therefore, Starting subscript 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 median is less than the unknown origin value, the value of the generational difference 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 is, $arr [3]-$arr [5] $start =$ mid+1; }else{//found, return the unknown origin value subscript return $mid;}} The binary lookup 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 value of the difference is on the right side of the middle item, therefore, End subscript unchanged, the starting subscript becomes the intermediate 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 value of the generational difference is on the left side of the middle item, therefore, the starting subscript is unchanged, the end subscript becomes the middle subscript minus 1, the first search is $arr[0]-$arr [5], the next//second search is, $arr [0]-$arr [1] $end = $mid-1; }else{//found, return the unknown origin value subscript return $mid;}} } Echo Searchpart2 ($arr, 5). ' <br> '; Echo searchpart2 ($arr 2,5);?>

PHP binary search algorithm Implementation
recently collated the knowledge of the previous learning algorithms, although the web development of 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 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 idea
divides n elements into roughly the same number of halves, taking A[N/2] compared to the X to find, and if X=A[N/2] finds X, the algorithm terminates. If X<A[N/2], we simply continue to search for x in the left half of array a (assuming that the array elements are in ascending order). If X>A[N/2], then we just continue to search for x in the right half of array A. The application of the
binary search method is very extensive, 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 the binary search algorithm for PHP

/** * binary search Algorithm * * @param array $arr ordered array * @param int $val value found * @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, $); 

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.