-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearches.py
146 lines (105 loc) · 3.48 KB
/
searches.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class SearchAlgorithms:
def __init__(self):
pass
def linear(self, s_list, n):
for i in range(len(s_list)):
if s_list[i] == n:
return i
return -1
def binary(self, s_list, n, min_at, max_at):
"""
:param s_list: Sorted List which is to be searched from
:param n: Element to be searched
:param min_at: Zero
:param max_at: Length of the list
:return: Index at which the element was found / -1 If element absent
"""
mid = (min_at + max_at) // 2
if s_list[mid] == n:
return mid
elif s_list[mid] > n:
return self.binary(s_list, n, min_at, mid - 1)
else:
return self.binary(s_list, n, mid + 1, max_at)
return -1
def exponential(self, s_list, n, e):
"""
:param s_list: List to be searched from
:param n: Element to be searched
:param e: Raise function to the power e
:return: Index at which the element was found / -1 If element absent
"""
if s_list[0] == n:
return 0
at = 1
while at < len(s_list) and s_list[at] <= n:
at = at * e
if at > len(s_list):
l = len(s_list)
else:
l = at
return self.binary(s_list[:l], n, 0, len(s_list[:l]))
def interpolation(self, s_list, n):
low, high = 0, (len(s_list) - 1)
while low <= high and s_list[low] <= n <= s_list[high]:
try:
at = low + int(((float(high - low) / (s_list[high] - s_list[low])) * (n - s_list[low])))
if s_list[at] == n:
return at
if s_list[at] < n:
low = at + 1;
else:
high = at - 1;
except ZeroDivisionError:
break
return -1
def hop(self, s_list, n, jump):
"""
:param s_list: List to be searched from
:param n: Element to be searched
:param jump: Make hops of how many elements?
:return: Index at which the element was found / -1 If element absent
"""
low, high = 0, 0
l = len(s_list)
while s_list[low] <= n and low < l:
if l - 1 > low + jump:
high = low + jump
else:
high = l - 1
if s_list[low] <= n <= s_list[high]:
break
low += jump;
if low >= l or s_list[low] > n:
return -1
if l - 1 > low + jump:
pass
else:
high = l - 1
i = low
while i <= high and s_list[i] <= n:
if s_list[i] == n:
return i
i += 1
return -1
def fibonacci(self, s_list, n):
neg_one, neg_two = 1, 0
fib = neg_one + neg_two
while fib < len(s_list):
neg_two, neg_one = neg_one, fib
fib += neg_two
at = -1
while fib > 1:
i = min(at + neg_two, (len(s_list) - 1))
if s_list[i] < n:
fib, neg_one = neg_one, neg_two
neg_two, at = fib - neg_one, i
elif s_list[i] > n:
fib = neg_two
neg_one -= neg_two
neg_two = fib - neg_one
else:
return i
if neg_one and at < (len(s_list) - 1) and s_list[at + 1] == n:
return at + 1;
return -1