二分查找

来源:互联网 发布:淘宝网壹号人家 编辑:程序博客网 时间:2024/06/09 18:07
def bsearch(a_list,item):    first=0    last=len(a_list)-1    found=False    while first<=last and not found:        midpoint=(first+last)//2        if a_list[midpoint]==item:            found=True        else:            if item<a_list[midpoint]:                last=midpoint-1            else:                first=midpoint+1        return founddef binary_search(a_list,item):    if len(a_list)==0:        return False    else:        midpoint=len(a_list)//2        if a_list[midpoint]==item:            return True        else:            if item<a_list[midpoint]:                return binary_search(a_list[midpoint+1],item)            else:                return binary_search(a_list[midpoint+1],item)

0 0
原创粉丝点击