php實現二分查找算法
// $low and $high have to be integersfunction BinarySearch( $array, $key, $low, $high ) { if( $low > $high ) // termination case { return -1; }
$middle = intval( ( $low+$high )/2 ); // gets the middle of the array if ( $array[$middle] == $key ) // if the middle is our key { return $middle; } elseif ( $key < $array[$middle] ) // our key might be in the left sub-array { return BinarySearch( $array, $key, $low, $middle-1 ); } return BinarySearch( $array, $key, $middle+1, $high ); // our key might be in the right sub-array
}</pre>
本文由用戶 y3c5 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!