1、冒泡排序
a=[2,3,4,5,10,9,5,1,6,7] x=len(a) for i in range(x): flag=False for j in range(i+1,x): if a[i]>a[j]: temp=a[i] a[i]=a[j] a[j]=temp flag=True if not flag: #如果没有交换过,证明已经有序 break print(a)
2、简单选择排序
a=[2,3,4,5,10,9,5,1,6,7] x=len(a) for i in range(x): maxindex=i for j in range(i+1,x): if a[maxindex]<a[j]: maxindex=j if i!=maxindex: temp=a[i] a[i]=a[maxindex] a[maxindex]=temp print(a)