Skip to content

Commit

Permalink
Created Binarysearch.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclipsion99 authored Oct 15, 2022
1 parent 693cdf3 commit fc9eba3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Eclipsion99/Binarysearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
arr = list(map(int, input("Enter elements:").split()))
elem = int(input("Enter element to search:"))

def binary_search(L, x):

l = 0
r = len(L) - 1
m = 0
while l <= r:
m = (r + l) // 2
if L[m] < x:
l = m + 1
elif L[m] > x:
r = m - 1
else:
return m
return -1

res = binary_search(arr, elem)
if res == -1:
print("Element not found")
else:
print("Element found at index:",res)

0 comments on commit fc9eba3

Please sign in to comment.