php-search_interpolation

来源:互联网 发布:java程序设计基础 编辑:程序博客网 时间:2024/06/10 10:06
$data = array(4,6,7,8,14,55,67,145,218,237,284);$num = interpolationsearch(284);var_dump($num);function interpolationsearch($num){global $data;$count = count($data);$high = $count-1;$low = 0;while ($high >= $low){//$mid  = floor(($high+$low)/2);$mid = floor($low + ($num-$data[$low])*($high - $low)/($data[$high]-$data[$low]));if($num == $data[$mid]){return $mid;}elseif($num > $data[$mid]){$low = $mid + 1;}elseif($num < $data[$mid]){$high = $mid - 1;}}return false;}

0 0
原创粉丝点击