-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathInterpolationSearch.java
57 lines (52 loc) · 1.82 KB
/
InterpolationSearch.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
package com.thealgorithms.searches;
/**
* InterpolationSearch is an algorithm that searches for a target value within a sorted array
* by estimating the position based on the values at the corners of the current search range.
*
* <p>
* The performance of this algorithm can vary:
* - Worst-case performance: O(n)
* - Best-case performance: O(1)
* - Average performance: O(log(log(n))) if the elements are uniformly distributed; otherwise O(n)
* - Worst-case space complexity: O(1)
* </p>
*
* <p>
* This search algorithm requires the input array to be sorted.
* </p>
*
* @author Podshivalov Nikita (https://github.com/nikitap492)
*/
class InterpolationSearch {
/**
* Finds the index of the specified key in a sorted array using interpolation search.
*
* @param array The sorted array to search.
* @param key The value to search for.
* @return The index of the key if found, otherwise -1.
*/
public int find(int[] array, int key) {
// Find indexes of two corners
int start = 0;
int end = (array.length - 1);
// Since array is sorted, an element present
// in array must be in range defined by corner
while (start <= end && key >= array[start] && key <= array[end]) {
// Probing the position with keeping
// uniform distribution in mind.
int pos = start + (((end - start) / (array[end] - array[start])) * (key - array[start]));
// Condition of target found
if (array[pos] == key) {
return pos;
}
// If key is larger, key is in upper part
if (array[pos] < key) {
start = pos + 1;
} // If key is smaller, x is in lower part
else {
end = pos - 1;
}
}
return -1;
}
}