Commit d57c55b4 by BellCodeEditor

save project

parent efc5944e
Showing with 7 additions and 9 deletions
...@@ -9,17 +9,15 @@ num = random.choice(alist) ...@@ -9,17 +9,15 @@ 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
while low<=high: while low<=high:
mid = (low+high)//2 mid = (low+high)//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+=1 low=mid+1
elif guess>num:
high-=1
else: else:
print('没有') high = mid-1
binary_search(alist,num) return None
\ No newline at end of file a = binary_search(alist,num)
print(a)
\ 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