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