Binary search in python
l1=list()
n=int(input("Enter the no. of elements: "))
for e in range(n):
ele=int(input("Enter value for element: "))
l1.append(ele)
s=int(input("Enter number to be found: "))
l1.sort()
#defining lowest and highest index
l=0
h=n-1
while l<=h:
m=(l+h)//2
if l1[m]==s:
print(f"Found at index :{m} ")
break
elif l1[m]>s:
h=m-1
else:
l=m+1
else:
print("Not Found")
PYTHON FILE LINK
Hello
ReplyDelete