First, the concept: Two-point search also known as binary search, the advantages are less than the number of comparisons, Find Fast, average performance, the disadvantage is that the unknown origin table is ordered table, and insert delete difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent. First, suppose that the elements in the table are arranged in ascending order, comparing the keywords in the middle position of the table with the lookup keywords, and if they are equal, the lookup succeeds; otherwise, the table is divided into the front and the last two sub-tables with the intermediate positional records, and if the middle position record keyword is greater than the Find keyword, the previous child Otherwise, find the latter child table further. Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful at this time.
Second, the code: for unordered arrays in the following ways.
Header ("content-type:text/html;charset= ' Utf-8 '"); function Twosearchmethod ($arr, $val, $left, $right) {if ($left >$ right) {echo "Cannot find the value"; 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 (1,9,3,4,5,6,7), sort ($arr);p Rint_r ($arr), echo "<br/>", $val =1;twosearchmethod ($arr, $val, 0, 6);