冒泡排序 python 相邻两个数对比 把大的数往后移动 第一轮把最大的数移动到倒一位置 第二轮把第二大的移动到倒数第二个 以此类推
a=[1,22,233,12,3,4]
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
#print alist,passnum
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp # 可改写成 alist[i] ,alist[i+1] = alist[i+1] , alist[i]
return alist
print bubbleSort(a)