插值查找,有序表的一种查找方式。插值查找基于二分查找,将查找点的选择改进为自适应选择,提高查找效率。
适用条件:适合于关键字值分布均匀的集合。
原理:
JAVA代码实现:
public class cha_zhi_cha_zhao {
public static void main(String[] args) {
int arr[] = { 0,1,2,3,4,5,6,7,8,9};
int length=arr.length;
int key=2;
int low = 0;//低位下标
int high = length-1;//高位下标
int mid;//中间值下标
while (low <= high)
{
mid =low+ (high-low)*(key - arr[low]) / (arr[high] - arr[low]);//插值
System.out.println((high-low)*(key - arr[low]) / (arr[high] - arr[low])+" "+low+" "+mid);
if (key < arr[mid])
{
high = mid - 1;
}
else if(key > arr[mid])
{
low = mid + 1;
}
else
{
System.out.println(mid);
break;
}
}
}
}
参考:https://blog.csdn.net/qq_29542611/article/details/79392661