-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathbinary_search.py
57 lines (43 loc) · 1.48 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
#
# Binary search works for a sorted array.
# Note: The code logic is written for an array sorted in
# increasing order.
# T(n): O(log n)
#
# A binary_search function. It returns
# location of query in given array array[l..r] is present,
# otherwise -1
# Python program to implement Binary Search
def binary_search(array, query):
lo, hi = 0, len(array) - 1
while lo <= hi:
mid = lo + (hi - lo) // 2
val = array[mid]
# If the element is present at the middle itself
if val == query:
return mid
# If element is smaller than mid, then
# it can only be present in right subarray
elif val < query:
lo = mid + 1
# Else the element can only be present
# in left subarray
else:
hi = mid - 1
# We reach here when element is not
# present in array
return None
def main():
array = [1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6]
print(array)
print("-----SEARCH-----")
print("found: ", 5, " in index:", binary_search(array, 5))
print("-----SEARCH-----")
print("found: ", 6, " in index:", binary_search(array, 6))
print("-----SEARCH-----")
print("found: ", 7, " in index:", binary_search(array, 7))
print("-----SEARCH-----")
print("found: ", -1, " in index:", binary_search(array, -1))
print("-----SEARCH-----")
if __name__ == "__main__":
main()