s
is a list. binary(s, 0, len(s) - 1, find)
is the initial call.Function returns an index of the queried item. If there is no such item it returns -1
.
def binary(s,p,q,find):
if find==s[(p+q)/2]:
return (p+q)/2
elif p==q-1 or p==q:
if find==s[q]:
return q
else:
return -1
elif find < s[(p+q)/2]:
return binary(s,p,(p+q)/2,find)
elif find > s[(p+q)/2]:
return binary(s,(p+q)/2+1,q,find)