Commit 234f82b0 by BellCodeEditor

auto save

parent b250abfe
Showing with 23 additions and 2 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
def chazhao(list1, n):
low = 0
high = len(list1) - 1
while high>=low:
mid = (low + high) //2
cai = list1[mid]
if cai == n:
return mid #返回下标位置
elif cai < n:
low = mid + 1
else:
high = mid - 1
return -1 #表示没找到
result = chazhao(num_list, 17)
if result < 0:
print("没找到")
else:
print("找到了,下标为"+str(result))
\ No newline at end of file
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