当前位置:网站首页>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 :
边栏推荐
- [hard core science popularization] working principle of dynamic loop monitoring system
- Ebpf cilium practice (1) - team based network isolation
- Analysis of maker education in innovative education system
- Implementation of navigation bar at the bottom of applet
- Leetcode 1984. Minimum difference in student scores
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- 数据中台落地实施之法
- Obsidan之数学公式的输入
- [Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
猜你喜欢
Data type - integer (C language)
Arm GIC (IV) GIC V3 register class analysis notes.
One click deployment of highly available emqx clusters in rainbow
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
PVTV2--Pyramid Vision TransformerV2学习笔记
Using nocalhost to develop microservice application on rainbow
Rainbow combines neuvector to practice container safety management
Iptables' state module (FTP service exercise)
为什么要选择云原生数据库
随机推荐
GFS分布式文件系统
GFS distributed file system
基本数据类型和string类型互相转化
路由信息协议——RIP
Leetcode 1984. Minimum difference in student scores
Input and output of floating point data (C language)
Download and install orcale database11.2.0.4
iptables 之 state模块(ftp服务练习)
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
Implementation method of data platform landing
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Opencv learning note 3 - image smoothing / denoising
How to understand distributed architecture and micro service architecture
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Compilation and linking of programs
2-3 lookup tree
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
Arm GIC (IV) GIC V3 register class analysis notes.
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”