#实现一个二分查找
#输入:一个顺序表list和要找的元素
#输出:待查找的元素的位置
def binary_search(list,item):
low = 0
high = len(list) - 1
while low <= high:
mid = (low + high) // 2
guess = list[mid]
if guess == item:
return mid
if guess < mid:
high = mid - 1
else:
low = mid + 1
return None
test = [0, 1, 2, 8, 13, 17, 19, 32, 42]
print(binary_search(test,13))
Python3&数据结构之二分查找
猜你喜欢
转载自blog.csdn.net/qq_38575545/article/details/84594263
今日推荐
周排行