您好,欢迎访问一九零五行业门户网

php实现二分查找算法

// $low and $high have to be integers function 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 { return binarysearch( $array, $key, $low, $middle-1 ); } return binarysearch( $array, $key, $middle+1, $high ); // our key might be in the right sub-array}
复制代码
php
其它类似信息

推荐信息