Skip to content

Commit f2fd818

Browse files
committedOct 15, 2022
Adding Binary_search.py
1 parent b477a43 commit f2fd818

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎Binary_search.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def binary_search(arr, x):
2+
low = 0
3+
high = len(arr) - 1
4+
mid = 0
5+
6+
while low <= high:
7+
8+
mid = (high + low) // 2
9+
10+
# If x is greater, ignore left half
11+
if arr[mid] < x:
12+
low = mid + 1
13+
14+
# If x is smaller, ignore right half
15+
elif arr[mid] > x:
16+
high = mid - 1
17+
18+
# means x is present at mid
19+
else:
20+
return mid
21+
22+
# If we reach here, then the element was not present
23+
return -1
24+
25+
26+
27+
#__main()__
28+
L=list(map(int,input().split()))
29+
p=int(input())
30+
L.sort()
31+
P=binary_search(L,p)
32+
if(P==-1):
33+
print("Not found")
34+
else:
35+
print("Position of element found: ",P)

0 commit comments

Comments
 (0)
Please sign in to comment.