Php binary search algorithm detail_php tutorial

Source: Internet
Author: User
Php binary search algorithm details. Php binary search algorithm explanation 1. concept: binary search, also known as semi-query, has the advantage of a relatively small number of times, fast search speed, and good average performance; the disadvantage is that the table to be queried must be an ordered table.

I. concept: binary search, also known as semi-query, has the advantage of a small number of queries, fast query speed, and good average performance. Its 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. First, assume that the elements in the table are arranged in ascending order and the keywords recorded in the middle of the table are compared 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. Repeat the preceding process until you find a record that meets the conditions to make the search successful, or until the child table does not exist, the search fails.

II. Code: Use the following method for unordered arrays.

Header ("Content-type: text/html; charset = 'utf-8'"); function twosearchmethod ($ arr, $ val, $ left, $ right) {if ($ left> $ right) {echo "this value cannot be found"; return ;}$ middle = round ($ left + $ right)/2 ); if ($ arr [$ middle]> $ val) {twosearchmethod ($ arr, $ val, $ left, $ middle-1 );} elseif ($ arr [$ middle] <$ val) {twosearchmethod ($ arr, $ val, $ middle + 1, $ right);} else {echo $ middle ;}} $ arr = array (, 7); sort ($ arr); print_r ($ arr); echo"
"; $ Val = 1; twosearchmethod ($ arr, $ val, 0, 6 );

Http://www.bkjia.com/PHPjc/859802.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/859802.htmlTechArticlephp binary search algorithm to explain a, concept: binary search, also known as half-fold search, advantage is less than the number of times, fast search, the average performance is good; the disadvantage is that the table to be queried must be an ordered table...

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.