-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search.py
39 lines (32 loc) · 915 Bytes
/
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
#find a element in an sorted array using binary search
def binary_search(arr, l, r, x):
if r >= l:
mid = int((l+r) /2)
if arr[mid] == x:
return mid
elif x > arr[mid]:
l = mid + 1
return binary_search(arr, l, r, x)
else:
r = mid - 1
return binary_search(arr, l, r, x)
else:
return -1
#arr = [1, 2, 3, 4, 5]
print("no of elements to enter")
n = int(input())
arr = list()
for i in range(0,n):
num = int(input())
arr.append(num)
print (arr)
while True:
print ("element to find")
x = input()
if x == "":
continue
result = binary_search(arr, 0, len(arr)-1, int(x))
if result == -1:
print (str(x) + " not found")
else:
print ("Element is present at index "+str(result))