Commit 44457706 by BellCodeEditor

save project

parent b250abfe
Showing with 37 additions and 2 deletions
import random
# 使用二分查找法,找出9和20在列表里面的索引 # 使用二分查找法,找出9和20在列表里面的索引
num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21] num_list = [1, 3, 5, 8, 11, 15, 17, 18, 20, 21]
\ No newline at end of file testList = [1371, 102841, 1324, 124, 5432, 124, 20, 9]
def BubbleSort(list):
secondList = list
for i in range(0, len(list) - 1):
for index in range (0, len(list) - 1):
if secondList[index] > secondList[index + 1]:
secondList[index], secondList[index + 1] = secondList[index + 1], secondList[index]
return secondList
def BinarySearch(findNum, searchList):
usingList = searchList
usingList = BubbleSort(usingList)
high = len(usingList) - 1
low = 0
while high > low:
mid = (high + low) // 2
num = usingList[mid]
if num == findNum:
return mid
elif num > findNum:
high = mid - 1
else:
low = mid + 1
return None
print(BinarySearch(9, testList))
print(BinarySearch(20, testList))
print(testList)
\ 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