冒泡排序
2020-05-04 本文已影响0人
hedy_
#随机生成数字进行冒泡排序
#冒泡:相邻两数进行比较,大的向后移,小的向前移
import random
def randomList(n):
nums = []
for i in range(n):
nums.append(random.randrange(10000))
return nums
iList=randomList(10)
# print(list_num)
def bubbleSort(iList):
if len(iList)<1:
return iList
else:
for iin range(1,len(iList)):
for jin range (0,len(iList)-i):
if iList[j] >= iList[j+1]:
iList[j],iList[j+1] = iList[j+1],iList[j]
print("第 %d轮交换结果:" %i,end="")
print(iList)
return iList
print(iList)
print(bubbleSort(iList))