二分查找--python实现

示例:

l = [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 fun(l,num,start = 0 ,end = 0):
    start = start if start else 0
    end = len(l) -1 if end == 0 else end
    mid = (end -start )//2 + start
    if start > end :
        return None
    elif l[mid] > num :
        return fun(l,num,start,mid-1)
    elif l[mid] < num :
        return fun(l,num,mid+1,end)
    elif l[mid] == num :
        return mid

ret = fun(l,35)
print(ret)

 

posted @ 2018-08-05 11:18  auxiaoliu  阅读(417)  评论(0编辑  收藏  举报