您的位置:首页 > 其它

Interpolation Search与BinarySearch的比较

2015-06-02 21:48 190 查看

InterpolationSearch的实现以及与BinarySearch的比较

关于BinarySearch的实现可以看这里

但是,上面关于BinarySearch的实现我们采用的是递归调用的方式来实现的,为了方便我们对比这两种算法的区别,这里我将BinarySearch用迭代来实现:代码如下:

private static int binarySearch(int[] a, int low,int high,int x) {
        int middle;
        while(low<=high){//注意:这里要有“=”号
            middle=low+(high-low)/2;//与InterpolationSearch的区别
            if(a[middle]==x){
                return middle;

            }
            else if(a[middle]>x){
                high=middle-1;

            }
            else{
                low=middle+1;
            }

        }
        return -1;
    }


而InterpolationSearch的java实现代码如下:

package org.wrh.algorithmimplements;

import java.util.Arrays;
import java.util.Scanner;

//插入排序的实现
public class InterpolationSearch {

    public static void main(String[] args) {
        int []a={10,21,23,42,56,78,98};
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入要查找的数字:");
        int x=sc.nextInt();
        int index=interpolation(a,0,a.length-1,x);
        if(index==-1){
            System.out.println("在数组"+Arrays.toString(a)+"中,没有找到"+x);
        }
        else{
            System.out.println(x+"在数组"+Arrays.toString(a)+"中的位置为:"+index);
        }

    }
/*
*  下面函数参数的说明:low为数组中最低下标,high为数组中的最大下标,
* *  x为要查找的树
*/
    private static int interpolation(int[] a, int low,int high,int x) {
        int middle;
        while(low<=high){
            middle=low+(high-low)*(x-a[low])/(a[high]-a[low]);//与BinarySearch的唯一一点区别,在**这里**
            if(a[middle]==x){
                return middle;

            }
            else if(a[middle]>x){
                high=middle-1;

            }
            else{
                low=middle+1;
            }

        }
        return -1;
    }

}


从代码上来看,InterpolationSearch和BinarySearch的唯一的区别在于划分时的中间值middle的取法不一样,BinarySearch中

middle=low+(high-low)/2;


而InterpolationSearch中时这样的:

middle=low+(high-low)*(x-a[low])/(a[high]-a[low]);


对比可知,InterpolationSearch中充分利用了要所要查找的X的更多的信息,根据X在数组中的大小比例来对数组进行划分,这样使得查找的速度更快。

总结

InterpolationSearch的时间复杂度为:O(log(logn)),而BinarySearch的时间复杂度为:O(logn),因此在对已排序的数组查找,InterpolationSearch要快

So why isn’t this used in practice? Probably because lg N is already really small. After all, if you have an array of length 2^32 this only drops you from ~32 to ~5 comparisons which in practical terms probably isn’t a big speed up for searching arrays.参考于这里



最后要说明的一点是:算法的思想在这里,用其他语言来实现也是挺容易的,只需要稍加改动就ok了。

修正:在上面的while循环中

while(low<=high)//注意有“=”号


这样才能保证数组序列中的第“0”号位置和第“length-1”的位置的元素被正确找到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: