-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Interpolation_Search.py
96 lines (75 loc) · 2.24 KB
/
Interpolation_Search.py
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
# INTERPOLATION SEARCH
# If the searching element is present in array,
# then it returns index of it, else returns -1
def interpolationSearch(arr, size, search_element):
# Assigning indexes
low = 0
high = size - 1
# As array is going to be sorted
# an element present in array must be in range defined by corner
while low <= high and search_element >=
arr[low] and search_element <= arr[high]:
if low == high:
if arr[low] == search_element:
return low
return -1
position = low + int(float(high - low) /
(arr[high] - arr[low])*(search_element - arr[low]))
# Condition of element found
if arr[position] == search_element:
return position
# If x is larger, x is in upper part
if arr[position] < search_element:
low = position + 1
else:
# If x is smaller, x is in lower part
high = position - 1
return -1
# Starting with the main code
# Creating array of the size n
# Size would be taken into the variable n from user
def main():
arr = []
size = int(input('Enter the size of array : '))
# Entering the elements in the array
print ('Enter elements :')
for index in range(0, size):
ele = int(input())
arr.append(ele)
print ('The elements entered are:\n', arr, '\n')
# Sorting our array
print ('The sorted array is: ')
arr.sort()
print (arr, '\n')
# Entering the searching element
search_element = int(input('Enter the element to be searched: '))
index = interpolationSearch(arr, size, search_element)
# If element was found
if index != -1:
# Returning the index of the element
print ('Element found at index', index)
else:
# Returning -1 as element has not found
print ('Element not found')
main()
""""
SAMPLE INPUT:
5
1
5
7
2
9
7
SAMPLE OUTPUT:
Enter the size of array : Enter elements :
The elements entered are:
[1, 5, 7, 2, 9]
The sorted array is:
[1, 2, 5, 7, 9]
Enter the element to be searched: Element found at index 3
Time Complexity of Interpolation Search Algorithm
Average Case: O (log log n)
Worst Case: O(N)
Space Complexity of Interpolation Search Algorithm is O(1)
"""