Commit 28500c85 by BellCodeEditor

save project

parent 5d060214
Showing with 11 additions and 10 deletions
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
g=82 def bs(a,n):
l=0 l=0
h=9 h=len(a)-1
m=(l+h)//2 m=(l+h)//2
while True: while True:
if num_list[m]==g: if num_list[m]==n:
print('找到了') return m
break
elif l>h: elif l>h:
print('没找到')
break break
elif g>num_list[m]: elif n>num_list[m]:
l=m+1 l=m+1
m=(l+h)//2 m=(l+h)//2
else: else:
h=m-1 h=m-1
m=(l+h)//2 m=(l+h)//2
j=bs(num_list,3)
print(j)
\ 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