Commit e6fc5b31 by BellCodeEditor

save project

parent 7df53d9b
Showing with 23 additions and 0 deletions
import random
list_a=[]
for i in range(1,101):
list_a.append(i)
a_num=random.choice(list_a)
def binary_search(alist,num):
low=0
high=len(alist)-1
while True:
mid=(low+high)//2
guess=alist[mid]
if guess==num:
print('找到了')
return mid
elif guess<num:
low=mid+1
else:
high=mid-1
s=binary_search(list_a,a_num)
print(s)
\ No newline at end of file
...@@ -3,6 +3,7 @@ num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] ...@@ -3,6 +3,7 @@ num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
low=0 low=0
high=9 high=9
num=20 num=20
def binary_search(alist,num):
while True: while True:
mid=(low+high)//2 mid=(low+high)//2
guess=num_list[mid] guess=num_list[mid]
......
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