Commit d2ba89cd by BellCodeEditor

save project

parent a68c46d7
Showing with 13 additions and 13 deletions
...@@ -6,18 +6,17 @@ for i in range(1, 101): ...@@ -6,18 +6,17 @@ for i in range(1, 101):
num = random.choice(alist) num = random.choice(alist)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num):
high=len(alist)-1 high=len(alist)-1
low=0 low=0
while low <= high: while low <= high:
mid=(low+high)//2 mid=(low+high)//2
zjz=num_list[mid] zjz=num_list[mid]
if mum == zjz: if mum == zjz:
print("找到了,索引是",mid) print("找到了,索引是",mid)
exit() exit()
elif cz < zjz: elif cz < zjz:
high=mid-1 high=mid-1
else: else:
low=mid+1 low=mid+1
print("没有这个数") print("没有这个数")
\ 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