Commit e917ab6e by BellCodeEditor

auto save

parent adaf6f8a
Showing with 15 additions and 0 deletions
...@@ -7,3 +7,17 @@ num = random.choice(alist) ...@@ -7,3 +7,17 @@ num = random.choice(alist)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num): def binary_search(alist,num):
low_kun = 0
high_kun = len(alist)-1
while high_kun >= low_kun:
mid_kun = (low_kun+high_kun)//2
if alist[mid_kun] == num:
return mid_kun
elif alist[mid_kun] < num:
low_kun = mid_kun + 1
else:
high_kun = mid_kun - 1
return None
result = binary_search(alist,num)
print("随机数是",num)
print("索引是",result)
\ 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