Commit 8ea552f9 by BellCodeEditor

auto save

parent adaf6f8a
Showing with 17 additions and 3 deletions
...@@ -4,6 +4,21 @@ alist = [] ...@@ -4,6 +4,21 @@ alist = []
for i in range(1, 101): for i in range(1, 101):
alist.append(i) alist.append(i)
num = random.choice(alist) num = random.choice(alist)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置szszszszszszszszszss唐老师
def binary_search(alist,num): def binary_search(alist,num):
\ No newline at end of file step = 0 # 记录查找次数
flag1 = len(alist)-1 #目标区域左边界
while step <= flag1: #区间数据范围小于1则结束循环
mid = (step + flag1) // 2 # 中间值
cds = alist[mid]
if cds == num:
return mid
elif cds < num:
step = mid + 1 #左边界后移
else:
flag1 = mid - 1
a = binary_search(alist,num)
print(num)
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