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
posted @ 2018-04-23 19:50  想翻身的猫  阅读(233)  评论(0编辑  收藏  举报