Commit e85ddb0c by BellCodeEditor

save project

parent 93787f28
Showing with 15 additions and 15 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
lxy = 0
dsq = len(num_list) - 1
while True:
mid = (lxy + dsq) // 2
guess = num_list[mid]
if guess == 20:
print("找到了")
break
elif dsq < lxy:
print("没有这个内容")
break
elif guess < 20:
lxy = mid + 1
else:
dsq = mid - 1
def binary_search(alist,num):
lxy = 0
dsq = len(alist) - 1
while True:
mid = (lxy + dsq) // 2
guess = alist[mid]
if guess == num:
return mid
elif dsq < lxy:
print("没有这个内容")
break
elif guess < num:
lxy = mid + 1
else:
dsq = mid - 1
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