我怎么知道我的二进制搜索是否有效?

时间:2018-11-29 09:41:13

标签: python-3.x

所以我正在写一个二进制搜索代码,这就是我所拥有的,但是我不知道出了什么问题,或者不知道它是否有效?

book = ["Archaeology", "Art", "Biology", "Chemistry", "Computing", "English", "French", "Geography", "History", "Maths", "Psychology"]

def binarySearch(book, find):
    found = False
    left = 0
    right = len(book)-1
    find = 'Geography'

    while left <= right and not found:
        midpoint = (right - left)//2
        if book[midpoint] == find:
            found = True
        else:
            if find < book[midpoint]:
                right = midpoint - 1
            else:
                left = midpoint + 1
    return found

0 个答案:

没有答案