‘‘‘ 冒泡排序:比较相邻元素,顺序错误就交换顺序 ‘‘‘ import random import cProfile def bubble_Sort(nums): for i in range(len(nums)-1): for j in range(i+1,len(nums)): if nums[i] > nums[j]: # max = nums[i] # nums[i] = nums[j] # nums[j] = max #python有更好的写法 nums[i],nums[j] = nums[j],nums[i] return nums nums = [5,6,3,2,1,65,2,0,8,0] nums = [random.randint(0,10000) for i in range(10000)] print(bubble_Sort(nums)) cProfile.run(‘bubble_Sort(nums)‘)
运行结果:
排序:略
运行时间:3.256秒(跟桶排序比差了不止一点- -)
原文地址:https://www.cnblogs.com/shuimohei/p/10380840.html
时间: 2024-10-01 00:28:55