python - 如何计时冒泡排序
问题描述
我不确定如何为这样的冒泡排序计时,感谢任何输入!此外,我希望能够“打印”排序后几秒钟内花费的时间。也欢迎对代码本身发表任何评论!
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()
解决方案
import time
def bubble_sort(list):
# Implementation of bubble sort
return list
#------------------------------------
def main():
l = [1,5,7,6,8]
start_time = time.time()
bubble_sort(l)
stop_time = time.time()
duration = stop_time - start_time()
#------------------------------------
main()
推荐阅读
- python - 如何从 extract_first() 输出中删除 xpath?
- macos - 如何在 MacOS WKWebView 中支持 WebAuthN?
- python-3.x - 如何解决熊猫中的“用户警告:未应用紧密布局:子图规范中的列数必须是彼此的倍数”
- google-cloud-platform - 将二进制数据从 Google Cloud Storage 流式传输到 Cloud Function
- c# - 我如何知道 C# Unity 中 AndroidJavaObject 类中的方法
- excel - VBA:剪切一系列列并将其粘贴到前三列数据的底部
- electron - 无法使用 Electron Builder 将图标添加到 Electron 应用程序
- docker - 码头工人无法启动烧瓶应用程序
- sql - 获取重复集
- javascript - ajax 数据表导出按钮不起作用