forked from bakiyaswanth/hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBInarySearch.py
More file actions
34 lines (24 loc) · 727 Bytes
/
BInarySearch.py
File metadata and controls
34 lines (24 loc) · 727 Bytes
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
# function
def BinarySearch(arr, left, right, key):
while left <= right:
mid = left +(right-1) // 2;
# check key is present at mid
if arr[mid] == key:
return mid
# if key is greater, ignore left half
elif arr[mid] < x:
left = mid + 1
# if key is smaller, ignore right half
else:
right = mid -1
# if element was not found
return -1
# Drived code
arr = [2, 4, 5, 9, 45,1, 6]
key = 5
# function call
result = BinarySearch(arr, 0, len(arr)-1, key)
if result != -1:
print("Element is present at index % d" % result)
else:
print("Element is not present in array")