Commit e521e0cb by BellCodeEditor

save project

parent e63de353
Showing with 13 additions and 4 deletions
...@@ -7,11 +7,20 @@ num = random.choice(alist) ...@@ -7,11 +7,20 @@ num = random.choice(alist)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num): def binary_search(alist,num):
low=0
high=len(alist)-1
mid=(low+high)//2
while low<=high: while low<=high:
low=0 mid=(high+low)//2
high=len(alist)-1 guess=alist[mid]
mid=(low+high)//2 if guess==num:
print('找到了',mid)
break
elif guess<num:
low=mid+1
else:
high=mid-1
binary_search(alist,10)
......
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