python实现二分查找算法,python二分算法,# low and hi


# low and high have to be integersdef binarySearch(array, key, low, high):    if low > high: # termination case        return -1    middle = (low + high) / 2 # gets the middle of the array    if array[middle] == key:  # if the middle is our key        return middle    elif key < array[middle]: # our key might be in the left sub-array        return binarySearch(array, key, low, middle-1)    else:                     # our key might be in the right sub-array        return binarySearch(array, key, middle+1, high)

评论关闭