【python】二分查找

2023-02-26 16:47:16 浏览数 (1)

文章目录

  • 二分查找Binary Search

二分查找Binary Search

终于做出来了,实在太生疏了,就一个=号,要了我的亲命! while low<=high:

代码语言:javascript复制
def binarysearch(list,a):
    print(list,a)
    low=0;
    high=len(list)-1
    a=int(a)
    while low<=high:
        mid =int( (low   high) / 2)
        print(mid,low,high)

        if list[mid]==a:
            return mid
        elif list[mid]>a:
            high=mid-1
        elif list[mid]<a:
            low = mid 1
if '__main__' == __name__:
    list = [2,6,8,9,14]
    a= 9
    print(binarysearch(list,a))

[2, 6, 8, 9, 14] 9
2 0 4
3 3 4
3

0 人点赞