-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathInterpolationSearchDemo.java
102 lines (87 loc) · 2.51 KB
/
InterpolationSearchDemo.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package ds_006_searching;
public class InterpolationSearchDemo {
public static void main(String[] args) {
// int[] array = { 1, 5, 6, 6, 6, 6, 6, 15, 17, 18, 21, 25, 32, 35, 39, 39, 39, 41, 42, 47, 47, 59, 63, 63, 64, 64, 69, 72 };
// checkIndex(array, 39);
// checkIndex(array, 40);
// checkIndex(array, 1);
// checkIndex(array, 72);
// int[] array = { 0, 4, 7, 9, 12, 14, 18, 25, 27, 36, 46, 50, 64, 79, 88 };
// checkIndex(array, 36);
int[] array = { 0, 7, 12, 17, 21, 26, 31, 33, 40, 43, 49, 51, 64, 66, 72, 73, 81, 87, 95, 99 };
checkIndex(array, 64);
checkIndex(array, 65);
checkIndex(array, 150);
checkIndex(array, -10);
checkIndex(array, 7);
checkIndex(array, 0);
checkIndex(array, 1);
checkIndex(array, 98);
checkIndex(array, 100);
checkIndex(array, 99);
}
private static int interpolationSearch(int[] array, int value) {
int min = 0;
int max = array.length-1;
if(value > array[max] || value < array[min]) {
return -1;
}
int indexRange = max - min;
int valueRange = array[max] - array[min];
int distance = value - array[min];
double fraction = ((double)distance)/(valueRange);
int guess = min + (int)(fraction*indexRange);
if(array[guess] == value) {
return guess;
} else {
if(array[guess] < value) {
min = guess;
int increment = 1;
while(array[guess] < value) {
// System.out.println("\t\t\tGuess: " + guess);
guess += increment;
increment *= 2;
}
// guess > value
max = guess;
// System.out.printf("\tMin: %d, Max: %d\n",min, max);
return binarySearch(array, value, min, max);
} else {
max = guess;
int increment = -1;
while(array[guess] > value) {
guess += increment;
increment *= 2;
}
// guess < value
min = guess;
// System.out.printf("\tMin: %d, Max: %d\n",min, max);
return binarySearch(array, value, min, max);
}
}
}
private static void checkIndex(int[] array, int value) {
int index = interpolationSearch(array, value);
if(index != -1) {
System.out.printf("%2d found at index: %2d\n", value, index);
} else {
System.out.printf("%2d not found!\n", value, index);
}
}
private static int binarySearch(int[] array, int value, int min, int max) {
while(min <= max) {
int mid = (min + max) / 2;
// System.out.printf("\tMin: %d, Mid: %d, Max: %d\n",min, mid, max);
if(array[mid] == value) {
return mid;
} else {
if( value < array[mid] ) {
max = mid-1;
} else {
min = mid+1;
}
}
}
return -1;
}
}