Skip to content

Commit 557575a

Browse files
authored
Binary Search BeeBombshell#109
takes input array from user and also the element to be searched
1 parent ad3cf0c commit 557575a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

binary_search.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def binarySearch(arr, l, r, x):
2+
if r >= l:
3+
mid = l + (r - l) // 2
4+
5+
if arr[mid] == x:
6+
return mid
7+
elif arr[mid] > x:
8+
return binarySearch(arr, l, mid-1, x)
9+
10+
else:
11+
return binarySearch(arr, mid + 1, r, x)
12+
13+
else:
14+
return -1
15+
16+
17+
arr = [int(i) for i in input().split())
18+
x = int(input())
19+
20+
result = binarySearch(arr, 0, len(arr)-1, x)
21+
22+
if result != -1:
23+
print("Element is present at index % d" % result)
24+
else:
25+
print("Element is not present in array")

0 commit comments

Comments
 (0)