递归方式实现二分查找

lst = [11,22,33,44,55,66,77,88,99]
def func(left,right,n):
    middle = (left + right) // 2
    if left > right:
        return - 1
    elif n > lst[middle]:
        left = middle + 1
    elif n < lst[middle]:
        right = middle - 1
    else:
        return print('cunzai',middle)
    return func(left,right,n)

func(0,len(lst) - 1,66)
# 递归的方式实现二分查找

 

posted on 2019-10-26 22:23  素心~  阅读(348)  评论(0编辑  收藏  举报

导航