-
Notifications
You must be signed in to change notification settings - Fork 497
/
binary_search.py
68 lines (58 loc) · 1.99 KB
/
binary_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
from random import randint
def binary_search(array, element):
"""
Perform Binary Search by Iterative Method.
:param array: Iterable of elements
:param element: element to search
:return: returns value of index of element (if found) else return None
"""
left = 0
right = len(array) - 1
while left <= right:
mid = (right + left) // 2
# indices of a list must be integer
if array[mid] == element:
return mid
elif array[mid] > element:
right = mid - 1
else:
left = mid + 1
return None
def binary_search_recursive(array, element, left=0, right=None):
"""
Perform Binary Search by Iterative Method.
:param array: Iterable of elements
:param left: start limit of array
:param right: end limit of array
:param element: element to be searched
:return: returns value of index of element (if found) else return None
"""
right = len(array) - 1 if right is None else right
if right >= left:
mid = (right + left) // 2
if array[mid] == element:
return mid
elif array[mid] > element:
return binary_search_recursive(array, element, left, mid - 1)
else:
return binary_search_recursive(array, element, mid + 1, right)
else:
return None
def main():
size = 100 # user can change it
domain = 100 # user can change it
array = [1, 9, 11, 13, 5, 7, 8, 5, 17, 1156, 114]
array.sort()
element = 13
result = binary_search_recursive(array, element)
if result is None:
print('Recursive Binary Search : Element not present in array')
else:
print('Recursive Binary Search : Element is present at index', result)
result = binary_search(array, element)
if result is None:
print('Iterative Binary Search : Element not present in array')
else:
print('Iterative Binary Search : Element is present at index', result)
if __name__ == '__main__':
main()