二分法查找(前提序列有序)

2018-09-13  本文已影响0人  地铁姑娘
#encoding=utf-8
def BinarySearch(arr,aimNum):
    start = 0
    end = len(arr)-1
    mid = int((start+end)/2)
    while True:
        if aimNum>arr[mid]:
            start = mid+1
            mid = int ((start + end) / 2)
        elif aimNum<arr[mid]:
            end = mid - 1
            mid = int ((start + end) / 2)
        else:
            return mid+1
            break
if __name__ == "__main__":
    aimAin = [1,2,12,45,78,90]
    print BinarySearch(aimAin,45)
上一篇下一篇

猜你喜欢

热点阅读