#!/usr/bin/python3
import random
# 创建无序的列表
def randomList(n):
iList = []
for i in range(n):
iList.append(random.randrange(1000))
return iList
iList = randomList(20)
def bubbleSort(iList):
print(iList)
if len(iList) <= 1:
return iList
for i in range(len(iList)):
for j in range(0,len(iList)-1):
if iList[j] >= iList[j+1]:
iList[j],iList[j+1] = iList[j+1],iList[j] # 交换位置
print('第 ' + str(i+1) + ' 次 ' ,end="")
print(iList)
return iList
if __name__ == "__main__":
bubbleSort(iList)
网友评论