-
Notifications
You must be signed in to change notification settings - Fork 2
/
sequentialbinarysearch.py
62 lines (47 loc) · 1.42 KB
/
sequentialbinarysearch.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
'''
This is a simple exercise to implement sequential and binary search in Python
'''
def sequential_search(lst, element):
"""
Searches an element inside a list and returns its index or -1 if not found
"""
index = -1
i = 0
for i, e in enumerate(lst):
if e == element:
return i
return -1
def binary_search(lst, element):
"""
Search an element in an ordered list using binary search. Return the index
if found or -1 otherwise
"""
left = 0
right = len(lst) - 1
while left < right:
mid = (right - left + 1) // 2
if lst[mid] == element:
return mid
else:
if element < lst[mid]:
right = mid - 1
else:
left = mid + 1
return -1
if __name__ == '__main__':
x = [1, 2, 3, 4, 5, 6, 7, 8]
try:
print(f'The array is {x}\nEnter the element to search (numbers only): ')
element = int(input())
print('Using which search algorithm? (1 for sequential, 2 for binary)')
algorithm = int(input())
if algorithm == 1:
position = sequential_search(x, element)
else:
if algorithm == 2:
position = binary_search(x, element)
else:
print('Only options 1 or 2!!!')
print(f'Output position: {position}')
except ValueError:
print('NUMBERS ONLY!!!')