php实现插值查找

<?php
$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;
}

?>
posted @ 2018-03-09 17:16  jintaonote  阅读(157)  评论(0编辑  收藏  举报