def BinarySearch(arr
, key
):
min = 0
max = len(arr
) - 1
if key
in arr
:
while True:
center
= int((min + max) / 2)
if arr
[center
] > key
:
max = center
- 1
elif arr
[center
] < key
:
min = center
+ 1
elif arr
[center
] == key
:
print(str(key
) + "在数组里面的第" + str(center
) + "个位置")
return arr
[center
]
else:
print("没有该数字!")
if __name__
== "__main__":
arr
= [1, 5, 9, 12, 26, 45, 49, 60, 63, 77, 81, 93]
while True:
key
= input("请输入你要查找的数字:")
if key
== " ":
break
else:
BinarySearch
(arr
, int(key
))
转载请注明原文地址: https://yun.8miu.com/read-22670.html