当前位置:网站首页>Interpolation lookup (two methods)
Interpolation lookup (two methods)
2022-07-07 08:37:00 【S dream chasing boy】
One 、 What is interpolation lookup
(1) Interpolation search algorithm is similar to binary search , The difference is that the interpolation lookup starts from The adaptive mid It's about Start looking for .
(2) Binary search mid The value is left and right The sum of the subscripts of the indicated sequence 1/2 namely mid = (left+right)/ 2.
(3) And interpolation search mid The value is calculated by formula , It is obvious from the formula mid The value of is not the middle position of the left and right indexes like dichotomy .
(4) Interpolation search formula ;3、int mid = low + (high - low) * (key - arr[low]) / (arr[high] - arr[low]) ;low Indicates the index on the left left,high Indicates the index on the right right. key Is the value you need to find .
Two 、 Features of interpolation search
(1) The search sequence must be ordered
(2) about Large amount of data And keywords Evenly distributed For the ordered sequence of , Interpolation search is faster .
(3) about Unevenly distributed For the ordered sequence of , This algorithm is not necessarily better than binary search .
3、 ... and 、 Code implementation
(1) Method 1 : recursive
public class InterpolationSearch {
public static void main(String[] args) {
int [] arr= {1,3,9,12,14,17,24,28,29};
int low = 0;
int high = arr.length-1;
int index = interpolationSearch(arr,low,high,17);
System.out.print(" Element found 17 The location of the for :"+index);
}
public static int interpolationSearch(int[] arr, int low, int high, int key) {
if (low > high) {
return -1;
}
int mid = low + (int) (1.0 * (key - arr[low]) / (arr[high] - arr[low]) * (high - low));
if (mid < low || mid > high) {
return -1;
}
if (arr[mid] == key) {
return mid;
} else if (key < arr[mid] ) {
return interpolationSearch(arr,low,mid - 1,key);
} else {
return interpolationSearch(arr,mid + 1, high,key);
}
}
}
Running results :

(2) Method 2 : iteration
public class InterpolationSearch {
public static void main(String[] args) {
int[] arr = { 1, 3, 9, 12, 14, 17, 24, 28, 29 };
int index = insertvaluesearch(arr, 17);
System.out.print(" Element found 17 The location of the for :" + index);
}
public static int insertvaluesearch(int arr[], int key) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) * (key - arr[low]) / (arr[high] - arr[low]);
if (arr[mid] == key) {
return mid;
}
if (arr[mid] < key) {
low = mid + 1;
}
if (arr[mid] > key) {
high = mid - 1;
}
}
return -1;
}
}Running results :

边栏推荐
- You should use Google related products with caution
- 下载和安装orcale database11.2.0.4
- [Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
- 【微信小程序:缓存操作】
- Golan idea IntelliJ cannot input Chinese characters
- How to realize the high temperature alarm of the machine room in the moving ring monitoring system
- Required String parameter ‘XXX‘ is not present
- SSM integration
- String operation
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
猜你喜欢

Novice entry SCM must understand those things

Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error

How to realize the high temperature alarm of the machine room in the moving ring monitoring system

Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster

联想混合云Lenovo xCloud:4大产品线+IT服务门户

Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers

Obsidan之数学公式的输入

2-3查找樹

Splunk query CSV lookup table data dynamic query

Automatic upgrading of database structure in rainbow
随机推荐
更改当前文件夹及文件夹下文件日期shell脚本
Several ways of lambda used in functions in kotlin (higher-order functions)
let const
Novice entry SCM must understand those things
The single value view in Splunk uses to replace numeric values with text
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
Using nocalhost to develop microservice application on rainbow
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
POJ - 3616 Milking Time(DP+LIS)
Qt Charts使用(重写QChartView,实现一些自定义功能)
PVTV2--Pyramid Vision TransformerV2学习笔记
Interpreting the practical application of maker thinking and mathematics curriculum
[Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
使用AGC重签名服务前后渠道号信息异常分析
Ebpf cilium practice (1) - team based network isolation
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
Interface as a parameter (interface callback)
Tuowei information uses the cloud native landing practice of rainbow
One click installation of highly available Nacos clusters in rainbow