Use PHP to implement binary search algorithm code sharing-PHP source code

Source: Internet
Author: User
Binary search, also known as semi-query, is an efficient search method. Binary search, also known as semi-query, is an efficient search method.


Method 1:

[Binary search requirements]: 1. the sequential storage structure must be used. 2. the values must be sorted by the keyword size.

[Advantages and disadvantages] the advantage of the half-lookup method is that the query speed is fast and the average performance is good. The disadvantage is that the table to be queried is an ordered table and it is difficult to insert or delete the table. Therefore, the half-fold lookup method is suitable for searching frequently ordered lists without frequent changes.

[Algorithm idea] first, compare the keywords recorded in the middle of the table with the search keywords. if the two are the same, the search is successful; otherwise, the table is divided into the first and last sub-tables by using the intermediate position record. if the keyword recorded in the middle position is greater than the search keyword, the former sub-table is further searched. Otherwise, the latter sub-table is further searched.

 $ X) {// if the value of the intermediate item is greater than the value to be queried, the difference value is on the left of the intermediate item. Therefore, the starting subscript remains unchanged, and the ending subscript is changed to the subscript of the intermediate item minus 1, the first search is $ arr [0]-$ arr [5], and the next search is $ end = $ mid-1; // $ arr [0]-$ arr [1]} elseif ($ arr [$ mid] <$ x) {// if the value of the intermediate item is smaller than the value to be queried, it indicates that the difference value is on the right of the intermediate item. Therefore, the ending subscript remains unchanged, and the starting subscript is changed to the subscript of the intermediate item 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 subscript of the value to be queried return $ mid ;}}} // perform the binary search function searchpart2 ($ arr, $ x) {$ start = 0; $ end = count ($ arr)-1 on the reverse sorted array; while ($ start <= $ end) {$ mid = intval ($ start + $ end)/2 ); // here, you only need to ensure that the calculation value of the subject under the intermediate item is an integer, or you can rounding it out without affecting the result if ($ arr [$ mid]> $ x) {// if the value of the intermediate item is greater than the value to be queried, it indicates that the difference value is on the right of the intermediate item. Therefore, the end subscript remains unchanged, and the start subscript is changed to the subscript of the intermediate item plus 1, the first search is $ arr [0]-$ arr [5]. the next search is $ start = $ mid + 1; // $ arr [3]-$ arr [5]} elseif ($ arr [$ mid] <$ x) {// if the value of the intermediate item is smaller than the value to be queried, it indicates that the difference value is on the left of the intermediate item. Therefore, the starting subscript remains unchanged, and the ending subscript is changed to the subscript of the intermediate item minus 1, the first search is $ arr [0]-$ arr [5]. the next search is, $ arr [0]-$ arr [1] $ end = $ mid-1;} else {// found, return the subscript of the value to be queried return $ mid ;}}} echo searchpart2 ($ arr, 5 ).'
'; Echo searchpart2 ($ arr2, 5) ;?>

PHP binary search algorithm implementation

Recently I sorted out my previous knowledge about algorithms. Although the algorithms used in WEB development are rare, I still back up some useful algorithms.

The half-lookup method is also called the binary lookup method. it fully utilizes the order relationship between elements and adopts the Grouping Policy. in the worst case, it can use O (log n) to complete the search task.

[Basic idea]

Divide n elements into two halves with roughly the same number. take a [n/2] For comparison with the x to be searched. if x = a [n/2], find x, algorithm termination. If xa [n/2], we only need to search for x in the right half of array.

Binary search is widely used and its ideas are 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 computer experts cannot write completely Correct binary search algorithms within two hours. The key to the problem is to accurately define the boundary of each search range and determine the termination conditions, and correctly summarize the various situations of parity, in fact, we can find that the specific algorithm is intuitive.

PHP binary search algorithm implementation

/*** Binary search algorithm ** @ param array $ arr ordered array * @ param int $ val: the value to be searched * @ return int. the returned array subscript exists, -1 */function binary_search ($ arr, $ val) {$ l = count ($ arr); // get the length of the ordered array $ 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, 33, 35, 56, 99); echo binary_search ($ arr, 57 );

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.