Interpolation Search与BinarySearch的比较
来源:互联网 发布:princeton算法公开课 编辑:程序博客网 时间:2024/05/08 00:41
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 0
- Interpolation Search与BinarySearch的比较
- Indexof与BinarySearch的比较
- 使用二分法Arrays.binarySearch()与list.contains()进行元素比较的效率分析
- 使用二分法Arrays.binarySearch()与list.contains()进行元素比较的效率分析
- 使用二分法Arrays.binarySearch()与list.contains()进行元素比较的效率分析
- Interpolation Search VS Binary Search
- solr与elastic search的比较
- 浅谈indexOf() 与 binarySearch()
- Interpolation Search 插值搜索算法
- 彩票两种简单方式:用数组保存7个数字,所有数字与以前的数字比较,如有重复重新生成binarySearch
- Freemarker的Interpolation
- Interpolation
- BinarySearch的实现
- Java的binarySearch
- 不断提高binarySearch的性能
- BinarySearch
- BinarySearch
- binarySearch
- 图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
- 使用dnsmasq快速搭建内网DNS
- LeetCode 198-House Robber
- ICA
- mybatis基础教程-学习
- Interpolation Search与BinarySearch的比较
- mongoDB 介绍(特点、优点、原理)
- git和svn的区别
- java4android(5)面向对象、类的定义、生成对象的方法、使用对象的方法、生成多个对象、构造函数super()、函数重载、static、继承
- maven 安装
- c# 面试经验
- C# Serializable
- Excel数据导入到SqlLite中
- 《转》收集最完整的arcgis拓扑处理