python 二分法O(logn)

def bin_search(data_set, val):
    low = 0
    high = len(data_set) - 1

    while low <= high:
        mid = (low + high) //2
        if data_set[mid] == val:
            return mid

        elif data_set[mid] <  val:
            low = mid + 1
        else:
            high = mid - 1

    return
def bin_search(data_set, val):
low = 0
high = len(data_set) - 1

while low <= high:
mid = (low + high) //2
if data_set[mid] == val:
return mid

elif data_set[mid] < val:
low = mid + 1
else:
high = mid - 1

return
原文地址:https://www.cnblogs.com/Liang-jc/p/8921387.html