递归,二分查找
递归,自己调用自己
二分查找就是 一半一半的查找
递归写二分查找
l2 = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
def two_index(lis,aim,star=0,end=None):
end = len(lis) - 1 if end is None else end
median = (end-star) // 2 +star
if star <= end:
if aim > lis[median]:
return two_index(lis,aim ,star=median+1,end=end)
if aim < lis[median]:
return two_index(lis,aim,star=star,end=median-1)
if aim == lis[median]:
return median
else:
return None
print(two_index(l2,100))
while 写二分查找
l2 = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
def two_index(lis,aim):
star=0
end=len(lis)-1
while star <= end:
median = (star + end) // 2
if aim == lis[median] :
return median
elif aim > lis[median]:
star = median + 1
elif aim < lis[median]:
end= median -1
return '查无此人'
print(two_index(l2,66))