Skip to content

Commit 01e6b07

Browse files
authored
Merge pull request deutranium#224 from Abhishek-Greninja17/patch-1
Create InterpolationSearch.java
2 parents 9391131 + 3809613 commit 01e6b07

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import java.util.*;
2+
/* Interpolation Search:
3+
Condition:
4+
array or vector should be sorted of n uniformly distributed value
5+
* sorted : in a sequence of either increasing or decreasing
6+
* uniformly distributed : difference between every consecutive element must be same
7+
* consecutive element difference : arr[8]-arr[7] = arr[2]-arr[1] = .. so on
8+
9+
terminolgy used:
10+
arr : int arr
11+
n : given element to be searched
12+
lo : starting index
13+
hi : ending index
14+
15+
Logic used:
16+
* recursion
17+
*/
18+
class InterpolationSearch {
19+
public static int interpolationSearch(int arr[], int lo,int hi, int x)
20+
{
21+
int pos;
22+
if (lo <= hi && x >= arr[lo] && x <= arr[hi])
23+
{
24+
pos = lo + (((hi - lo) / (arr[hi] - arr[lo])) * (x - arr[lo]));
25+
26+
if (arr[pos] == x)
27+
return pos;
28+
if (arr[pos] < x) //x is in right sub array as is closer to hi
29+
return interpolationSearch(arr, pos + 1, hi, x); //dividing in right half
30+
if (arr[pos] > x) //x is in left sub array as is closer to lo
31+
return interpolationSearch(arr, lo, pos - 1, x); //dividing in left half
32+
}
33+
return -1;
34+
}
35+
36+
// Driver Code
37+
public static void main(String[] args)
38+
{
39+
//test cases:
40+
/* to print the index of the Element searched in the array
41+
starting from 0 and to print -1 if the element is not
42+
found.
43+
44+
** diff : |arr[n]-arr[n-1]|
45+
*/
46+
47+
//test case 1 : all pos int with diff=1
48+
int arr1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
49+
int n = arr1.length;
50+
int x = 17; // Element to be searched
51+
int res = interpolationSearch(arr1, 0, n - 1, x);
52+
System.out.println(res);
53+
54+
//test case 2 : with negative numbers with diff=1
55+
int arr2[] = { -5,-4,-3,-2,-1,0,1,2,3,4,5,6,7};
56+
n = arr2.length;
57+
x = -2; // Element to be searched
58+
res = interpolationSearch(arr2, 0, n - 1, x);
59+
System.out.println(res);
60+
61+
//test case 3 : diff=5
62+
int arr3[] = { -15,-10,-5,0,5,10};
63+
n = arr3.length;
64+
x = 5; // Element to be searched
65+
res = interpolationSearch(arr3, 0, n - 1, x);
66+
System.out.println(res);
67+
68+
//test case 4 : element not found
69+
int arr4[] = { -15,-10,-5,0,5,10,15,20};
70+
n = arr4.length;
71+
x = -2; // Element to be searched
72+
res = interpolationSearch(arr4, 0, n - 1, x);
73+
System.out.println(res);
74+
75+
//conditions check:
76+
77+
//test case 5 : unsorted
78+
// unsorted thus will provide inconsistent solution (may give correct or may not)
79+
int arr5[] = {1,7,-8, 23, 6};
80+
n = arr5.length;
81+
x = 6; // Element to be searched
82+
res = interpolationSearch(arr5, 0, n - 1, x);
83+
System.out.println(res);
84+
85+
//test case 6 : variable difference
86+
// sorted not uniformly increasing or decreasing thus won't be able to find
87+
int arr6[] = {1,2,4,7,9,2};
88+
n = arr6.length;
89+
x = 7; // Element to be searched
90+
res = interpolationSearch(arr6, 0, n - 1, x);
91+
System.out.println(res);
92+
93+
//test case 7 : diff=0 (same elements)
94+
// sorted not uniformly increasing or decreasing thus will provide garbage or null value
95+
int arr7[] = {0,0,0,0,0,0,0,0,0,0,0};
96+
n = arr7.length;
97+
x = 17; // Element to be searched
98+
res = interpolationSearch(arr7, 0, n - 1, x);
99+
System.out.println(res);
100+
}
101+
}

0 commit comments

Comments
 (0)