We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b477a43 commit f2fd818Copy full SHA for f2fd818
Binary_search.py
@@ -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