data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6] # for j in range(len(data)): # for i in range(len(data)-1): # if data[i] > data[i+1]: # tmp = data[i] # data[i] = data[i+1] # data[i+1] = tmp # print (data) #外层循环次数太多 for j in range(1,len(data)): for i in range(len(data)-j): if data[i] > data[i+1]: tmp = data[i] data[i] = data[i+1] data[i+1] = tmp print (data) #精简了外层循环
时间: 2024-10-12 12:30:15