Commit be24c7db by BellCodeEditor

auto save

parent c41dfe9f
Showing with 4 additions and 3 deletions
# 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
num_list = [-1755674526,-382548333,-37552217,-4723559,-678678,-69345,-7937,-577,-34,-8,0,6,19,360,2902,56700,584000,4475663,89098765,758265987,1234543210]
def binary_search(alist,num):
low = 0
high = len(num_list)-1
high = len(alist)-1
while low<=high:
mid = (low+high)//2
guess = alist[mid]
......@@ -14,5 +14,5 @@ def binary_search(alist,num):
elif guess > num:
high = mid - 1
return None
resalt = binary_search(num_list,20)
resalt = binary_search(num_list,-678)
print(resalt)
\ 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