forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpolation Search.java
More file actions
31 lines (28 loc) · 847 Bytes
/
Copy pathInterpolation Search.java
File metadata and controls
31 lines (28 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.lang.Math ;
public class Search {
static int interpolation_search(int arr[],int key){
int start = 0 , end = arr.length -1 ;
while(start <= end && key >= arr[start] && key <= arr[end]){
if(start == end){
if(arr[start] == key) return start ;
return -1 ;
}
int pos = start + ((end - start) / (arr[end] - arr[start])*(key - arr[start]));
if(arr[pos] == key)
return pos ;
if(arr[pos] < key)
start = pos + 1 ;
else
end = pos - 1;
}
return -1 ;
}
public static void main(String ...s){
int arr[]= {1,3,5,7,9,13,19,30};
int key = 19;
int index = interpolation_search(arr, key);
System.out.println("-----INTERPOLATION SEARCH-----");
System.out.print("The key " + key + " ");
System.out.print((index == -1) ? "doesn't exist in the Array." : ("is present at index: " + index));
}
}