Code:
Note:- Scroll horizontally to see the full line of code.
def binary_search(a,target):
# a is list of intergers and target is key element to be searched
start=0
end=len(a)-1
while(start<=end):
mid=int((start+end)/2)
if(a[mid]==target):
print(target,"is present at index",mid)
break
elif(target>a[mid]):
start=mid+1
elif(target<a[mid]):
end=mid-1
else:
print(target,"not found in list")
Comments
Post a Comment