-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_algorithms.py
165 lines (142 loc) · 4.18 KB
/
search_algorithms.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#David P
#Nov 14, 2018
#Search algorithms w/ for loops, while loops, sentinel search, recrursion and iterative
#---------------------------------------------------------Linear Search Algorithms---------------------------------------------------------#
#1 - W/ while loop
def linear_search1(lst,value):
"""(list, object) -> int
Return the index of the first occurrence of value in lst, or return
-1 if value is not in lst.
>>> linear_search1([2, 5, 1, -3], 2)
0
>>> linear_search1([0, 5, 2, 1, -3], 2)
2
>>> linear_search1([2, 5, 1, -3], -3)
3
>>> linear_search1([2, 5, 1, -3], -5)
-1
>>> linear_search1([2, 5, 1, -3], 9)
-1
>>> linear_search1([2, 5, 1, -3], 4)
-1
>>> linear_search1([], 4)
-1
>>> linear_search1([1], 4)
-1
>>> linear_search1([4, 2, 5, 1, -3, 4], 4)
0
"""
index=0
while index < len(lst) and lst[index] != value:
index+=1
if index == len(lst):
return -1
return index
def linear_search2(lst,value):
"""(list, object) -> int
Return the index of the first occurrence of value in lst, or return
-1 if value is not in lst.
>>> linear_search2([2, 5, 1, -3], 2)
0
>>> linear_search2([0, 5, 2, 1, -3], 2)
2
>>> linear_search2([2, 5, 1, -3], -3)
3
>>> linear_search2([2, 5, 1, -3], -5)
-1
>>> linear_search2([2, 5, 1, -3], 9)
-1
>>> linear_search2([2, 5, 1, -3], 4)
-1
>>> linear_search2([], 4)
-1
>>> linear_search2([1], 4)
-1
>>> linear_search2([4, 2, 5, 1, -3, 4], 4)
0
"""
for index in range(len(lst)):
if lst[index]==value:
return index
return -1
def linear_search3(lst,value):
"""(list, object) -> int
Return the index of the first occurrence of value in lst, or return
-1 if value is not in lst.
>>> linear_search3([2, 5, 1, -3], 2)
0
>>> linear_search3([0, 5, 2, 1, -3], 2)
2
>>> linear_search3([2, 5, 1, -3], -3)
3
>>> linear_search3([2, 5, 1, -3], -5)
-1
>>> linear_search3([2, 5, 1, -3], 9)
-1
>>> linear_search3([2, 5, 1, -3], 4)
-1
>>> linear_search3([], 4)
-1
>>> linear_search3([1], 4)
-1
>>> linear_search3([4, 2, 5, 1, -3, 4], 4)
0
"""
lst.append(value)
index=0
while lst[index] != value:
index+=1
lst.pop()
if index == len(lst):
return -1
return index
#---------------------------------------------------------Binary Search Algorithms---------------------------------------------------------#
def binary_search_recursive(lst, value, left, right):
"""(list, object) -> int
Return the index of the first occurrence of value in lst, or return
-1 if value is not in lst.
>>> binary_search_recursive([-3, 1, 2, 5], 5, 0, 3)
3
>>> binary_search_recursive([2, 2, 4, 5], 2,0,3)
1
>>> binary_search_recursive([-3, 1, 2, 5], 4, 0, 3)
-1
>>> binary_search_recursive([], 5, 0, 0)
-1
"""
lst.sort()
if left > right or value not in lst:
return -1
middle = (left+right)//2
if lst[middle] == value:
return middle
if value > lst[middle]:
return binary_search_recursive(lst, value, middle+1, right)
return binary_search_recursive(lst, value, left, middle-1)
def binary_search_iterative(lst, value):
"""(list, object) -> int
Return the index of the first occurrence of value in lst, or return
-1 if value is not in lst.
>>> binary_search_iterative([-3, 1, 2, 5], 5)
3
>>> binary_search_iterative([2, 2, 4], 2)
1
>>> binary_search_iterative([-3, 1, 2, 5], 4)
-1
>>> binary_search_iterative([], 5)
-1
"""
lst.sort()
left, right = 0, len(lst)-1
while not left > right:
middle = (left+right)//2
if value == lst[middle]:
return middle
elif value < lst[middle]:
right = middle - 1
else:
left = middle + 1
return -1
if __name__ == '__main__':
import doctest
doctest.testmod()