Commit 28500c85 by BellCodeEditor

save project

parent 5d060214
Showing with 18 additions and 18 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: break
print('没找到') elif n>num_list[m]:
break l=m+1
elif g>num_list[m]: m=(l+h)//2
l=m+1 else:
m=(l+h)//2 h=m-1
else: m=(l+h)//2
h=m-1 j=bs(num_list,3)
m=(l+h)//2 print(j)
\ No newline at end of file \ 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