Article From:https://www.cnblogs.com/sea-stream/p/9689112.html
def binary_search(lis, key):
    low = 0
    high = len(lis) - 1
    time = 0
    while low < high:
        time += 1
        # 
        mid = low + int((high - low) * (key - lis[low])/(lis[high] - lis[low]))
        print("mid=%s, low=%s, high=%s" % (mid, low, high))
        if key < lis[mid]:
            high = mid - 1
        elif key > lis[mid]:
            low = mid + 1
        else:
            # 
            print("times: %s" % time)
            return mid
    print("times: %s" % time)
    return False
 
LIST = [1, 5, 7, 8, 22, 54, 99, 123, 200, 222, 444]
result = binary_search(LIST, 444)
print(result)

  

Link of this Article: Python interpolation search

Leave a Reply

Your email address will not be published. Required fields are marked *