当前位置:网站首页>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 :
边栏推荐
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
- 路由信息协议——RIP
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
- Arm GIC (IV) GIC V3 register class analysis notes.
- Input of mathematical formula of obsidan
- 如何在HarmonyOS应用中集成App Linking服务
- Teach you how to select PCB board by hand (II)
- Interface as a parameter (interface callback)
- Give full play to the wide practicality of maker education space
- Open3d ISS key points
猜你喜欢
联想混合云Lenovo xCloud:4大产品线+IT服务门户
Explore creativity in steam art design
PVTV2--Pyramid Vision TransformerV2学习笔记
如何在HarmonyOS应用中集成App Linking服务
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
随机推荐
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
In go language, function is a type
ES6_ Arrow function
JS的操作
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Golan idea IntelliJ cannot input Chinese characters
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
Several ways of lambda used in functions in kotlin (higher-order functions)
联想混合云Lenovo xCloud:4大产品线+IT服务门户
更改当前文件夹及文件夹下文件日期shell脚本
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Opencv learning notes 1 -- several methods of reading images
【无标题】
数据中台落地实施之法
IELTS review progress and method use [daily revision]
One click deployment of highly available emqx clusters in rainbow
Implementation of navigation bar at the bottom of applet
Arm GIC (IV) GIC V3 register class analysis notes.
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
Practice of implementing cloud native Devops based on rainbow library app