def search(alist,item,start=0,end=0): # print("%s-%s" % (start, end)) if start > end: return None mid = (start + end) // 2 if alist[mid] == item: return mid elif alist[mid] > item: return search(alist, item, start, mid - 1) else: return search(alist, item, mid + 1, end) def binary_search(alist,item): """二分查找""" return search(alist,item,0,len(alist) -1) if __name__ == '__main__': a = [1, 2, 3, 4, 5,6, 242, 823] ret = binary_search(a, 1) print(ret) ret = binary_search(a, 2) print(ret) ret = binary_search(a, 3) print(ret) ret = binary_search(a, 4) print(ret) ret = binary_search(a, 5) print(ret) ret = binary_search(a, 6) print(ret) ret = binary_search(a, 242) print(ret) ret = binary_search(a, 823) print(ret) ret = binary_search(a, 8) print(ret)