二分查找算法(简单版,修改):
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 func(l,aim,s=0,end=len(l)-1):
    mid = (len(l)-1)//2
    nextIndex = s + mid+1
    # lastM = mid
    # lastE= end
    # lastS = s
    if l:
        if aim > l[mid]:
            func(l[mid+1:],aim,nextIndex,end)
        elif aim < l[mid]:
            func(l[:mid],aim,s,nextIndex)
        elif aim == l[mid]:
            print("bingo",s+mid)
    else:
        print('找不到')
func(l,15)   # bingo 4
func(l,66)  # bingo 17

 

posted on 2018-08-23 15:00  yellwonfin  阅读(111)  评论(0编辑  收藏  举报