我不确定如何对这样的气泡排序计时,任何输入都是 谢谢!此外,我希望能够“打印”排序后几秒钟内所用的时间。任何关于代码本身的注释都是受欢迎的!在
def bubbleSort(list):
needNextPass = True
k = 1
while k < len(list) and needNextPass:
# List may be sorted and next pass not needed
needNextPass = False
for i in range(len(list) - k):
if list[i] > list[i + 1]:
# swap list[i] with list[i + 1]
temp = list[i]
list[i] = list[i + 1]
list[i + 1] = temp
needNextPass = True # Next pass still needed
# A test method
def main():
list = [5000, 200000, 250000, 10000, 150000, 300000]
bubbleSort(list)
for v in list:
print("Bubble sort list: ",v, end = ",")
main()
相关问题 更多 >
编程相关推荐