Commit b1439bba by BellCodeEditor

save project

parent e521e0cb
Showing with 3 additions and 6 deletions
...@@ -9,20 +9,17 @@ num = random.choice(alist) ...@@ -9,20 +9,17 @@ num = random.choice(alist)
def binary_search(alist,num): def binary_search(alist,num):
low=0 low=0
high=len(alist)-1 high=len(alist)-1
mid=(low+high)//2
while low<=high: while low<=high:
mid=(high+low)//2 mid=(high+low)//2
guess=alist[mid] guess=alist[mid]
if guess==num: if guess==num:
print('找到了',mid) return mid
break
elif guess<num: elif guess<num:
low=mid+1 low=mid+1
else: else:
high=mid-1 high=mid-1
binary_search(alist,10) s=binary_search(alist,10)
print('找到了',s)
......
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