-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathinterpolation_search.java
81 lines (60 loc) · 2.11 KB
/
interpolation_search.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.*;
class interpolation_search {
/*
-------------------------
problem - Given a sorted integer array and a target,
determine if the target exists in the array or not using an interpolation search algorithm.
If the target exists in the array, return the index of it.
------------------
it is same as binary search but it use this formula to calculate mid -
mid = low + ((target – A[low]) * (high – low) / (A[high] – A[low]));
------------------
variables -
start - starting index also know as low or low (low most element )
end - ending index also know as high or right ( right most element)
target - element to be search in the array
size - size of the array
----------------------
*/
//Function to determine if target exists in a sorted array `array` or not
public static int interpolationSearch(int array[], int start,
int end, int target)
{
int mid;
// search space is array[low…high]
if (start <= end && target >= array[start] && target <= array[end]) {
// estimate mid
mid = start+ (((end - start) / (array[end] - array[start]))
* (target - array[start]));
//target value found
if (array[mid] == target)
return mid;
//target is big then discard all elements in the left search space, including the middle element
if (array[mid] < target)
return interpolationSearch(array, mid + 1, end, target);
//target is small then discard all elements in the right search space, including the middle elemen
if (array[mid] > target)
return interpolationSearch(array, start, mid - 1, target);
}
//if target is not found
return -1;
}
// Driver Code
public static void main(String[] args)
{
// Array of items on which search will
// be conducted.
int arr[] = { 2, 5, 10, 14, 21, 26, 32, 37, 42, 49};
int size= arr.length;
// Element to be searched
int target = 18;
int index = interpolationSearch(arr, 0, size - 1, target);
// If element was found
if (index != -1)
System.out.println("Element found at index "
+ index);
else
System.out.println("Element not found.");
}
}
//code is contributed by rajendra mandliya