def binsearch(ar,key,low,high):
if low>high:
return -999 #Base Case
mid=int((low+high)/2)
if key==ar[mid]:
return mid #Base Case
elif key<ar[mid]:
high=mid-1
return binsearch(ar,key,low,high)
else:
low=mid+1
return binsearch(ar,key,low,high)
ary=[13,15,17,19,21,23,34,36,45]
item=int(input("Enter no. to
search:"))
res=binsearch(ary,item,0,len(ary)-1)
if
res>=0:
print(item,"Found at index",res)
else:
print("Number not found")
Comments
Post a Comment