forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpolation_search.py
More file actions
57 lines (41 loc) · 1.4 KB
/
interpolation_search.py
File metadata and controls
57 lines (41 loc) · 1.4 KB
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
"""
Interpolation Search
Search for a target value in a uniformly distributed sorted array by
estimating the position of the target using linear interpolation.
Reference: https://en.wikipedia.org/wiki/Interpolation_search
Complexity:
Time: O(1) best / O(log log n) average / O(n) worst
Space: O(1)
"""
from __future__ import annotations
def interpolation_search(array: list[int], search_key: int) -> int:
"""Search for *search_key* in a sorted *array* using interpolation search.
Args:
array: Sorted list of integers in ascending order.
search_key: Value to search for.
Returns:
Index of *search_key* in *array*, or -1 if not found.
Examples:
>>> interpolation_search([-25, -12, -1, 10, 12, 15, 20, 41, 55], -1)
2
>>> interpolation_search([5, 10, 12, 14, 17, 20, 21], 55)
-1
>>> interpolation_search([5, 10, 12, 14, 17, 20, 21], -5)
-1
"""
high = len(array) - 1
low = 0
while (low <= high) and (array[low] <= search_key <= array[high]):
pos = low + int(
((search_key - array[low]) * (high - low)) / (array[high] - array[low])
)
if array[pos] == search_key:
return pos
if array[pos] < search_key:
low = pos + 1
else:
high = pos - 1
return -1
if __name__ == "__main__":
import doctest
doctest.testmod()