二分查找

#nums list
#aim int
#return int
def binarySearch(nums,aim):
    if aim not in nums:
        return -1
    left = 0
    right = len(nums)-1
    while left<=right:
        mid = (left+right)/2
        if aim==nums[mid]:
            return mid
        elif aim>nums[mid]:
            left = mid+1
        else:
            right = mid-1

aimAin = [1,2,12,13,14,45,67,90,78,90,100]
print binarySearch(aimAin,13)

你可能感兴趣的:(二分查找)