__author__ = 'steven'

list = [1, 3, 6, 7, 9, 13, 15, 26, 33, 45]

def bin_search(num, list):
    lenth = len(list)
    start = 0
    end = lenth -1
    middle = int(( start + end) / 2)

    if lenth < 0:
        return "error"

    while start <= end:
        if num == list[middle]:
            return middle
        elif num > list[middle]:
            start = middle + 1
        else:
            end = middle - 1

        middle = int(( start + end) / 2)

    return "cannot find your number"

print(bin_search(15, list))

相关文章: