-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.py
More file actions
22 lines (20 loc) · 784 Bytes
/
Copy pathBinarySearch.py
File metadata and controls
22 lines (20 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class BinarySearch:
def binary_search(self, array, low, high, key):
while low <= high:
mid = (low + high) // 2
if key < array[mid]:
high = mid - 1
self.binary_search(array, low, high, key)
elif key > array[mid]:
low = mid + 1
self.binary_search(array, low, high, key)
else:
return "Found"
return "Not Found"
if __name__ == "__main__":
binary_search_obj = BinarySearch()
list1 = input("Enter Sorted list of values by space separated: ").split()
list1 = list(map(int, list1))
key_item = int(input("Enter key to search: "))
result = binary_search_obj.binary_search(list1, 0, len(list1)-1, key_item)
print(result)