二分查找

data = [1,7,17,18,27,29,30,35,35,39,41,63,78,82,82,91,92]

def get(data_list,target):
left = 0 #起始
right = len(data_list)-1 #终点 因有起始位置所以要减一
mid = int((left+right)/2) #中间位置

while left <= right: #循环
if data_list[mid] == target:
print(mid)
break
elif data_list[mid] > target:
left = mid - 1
elif data_list[mid] < target:
right = mid + 1
else:
print('没有这个数')


if __name__ == '__main__':
get(data, 39)
posted @ 2020-08-06 14:42  寂静音无  阅读(60)  评论(0编辑  收藏  举报