Commit 7ebf9e0e by BellCodeEditor

save project

parent b250abfe
Showing with 24 additions and 2 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file
def binarySearch(alist,bingoNum):
low = 0
high = len(alist)-1
while high > low:
mid = (low + high)//2
num = alist[mid]
if bingoNum == num:
return mid
elif num > bingoNum:
high = mid-1
else:
low = mid+1
return None
num_list = [1,8,5,12,9,16,13,20,17,24,21,28,25,29,39,33,40,50,44,54,48,58,54,3124,32,444,35,545,45,34,]
num_list.sort()
print(num_list)
guess = int(input("请输入需要查找的数"))
print(binarySearch(num_list,guess))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment