Interpolation Search与BinarySearch的比较

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”的位置的元素被正确找到

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
interpolation(插值)是指在数据点之间进行估计或者预测。在可视化中,插值可以用于平滑曲线或图像、填充缺失数据等。 在 Python 中,matplotlib 和 seaborn 库中的许多绘图函数都提供了 interpolation 参数,可用于控制插值方法。常用的插值方法包括: - ‘nearest’:最近邻插值,即使用最近的数据点作为估计值。 - ‘linear’:线性插值,即使用两个最近的数据点之间的线性关系进行估计。 - ‘cubic’:三次样条插值,即使用三次多项式拟合数据点来进行估计。 - ‘spline’:样条插值,是一种平滑的插值方法,可以使用一条平滑的曲线来拟合数据点。 下面是一个使用插值方法绘制函数图像的示例代码: ```python import numpy as np import matplotlib.pyplot as plt # 生成 x 和 y 数据 x = np.linspace(0, 2*np.pi, 21) y = np.sin(x) # 绘制原始数据的散点图 plt.scatter(x, y, color='r') # 绘制插值曲线 x_new = np.linspace(0, 2*np.pi, 100) y_new = np.interp(x_new, x, y) plt.plot(x_new, y_new, color='b', linestyle='--', linewidth=2, label='interpolation') # 显示图像 plt.legend() plt.show() ``` 在上面的代码中,使用 np.interp() 函数对原始数据进行插值,生成新的 x 和 y 数据,然后使用 plot() 函数绘制插值曲线。在 plot() 函数中,linestyle 参数设置线条样式,linewidth 参数设置线条宽度,label 参数设置图例标签。最后使用 legend() 函数添加图例,show() 函数显示图像。 您可以根据需要修改插值方法和其他参数,生成符合您需求的图像。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值