Python实现二分查找
程序员文章站
2024-03-19 16:12:04
...
话不多说,直接上代码:
def search(sequence,number,lower=0,upper=None):
if upper is None: upper = len(sequence) - 1
if lower== upper:
assert number == sequence[upper]
return upper
else:
middle = (lower + upper) //2
if number > sequence[middle]:
return serch(sequence,number,middle + 1,upper)
else:
return serch(sequence,number,lower,middle)