1 #!/usr/bin/python 2 #排序方法 3 #冒泡排序 4 def buble(l): 5 for i in range(len(l)): 6 for j in range(len(l)-i-1): 7 if l[j]>l[j+1]: 8 l[j+1],l[j]=l[j],l[j+1] 9 print l 10 #选择排序 11 def select(l): 12 for i in range(len(l)): 13 for j in range(i+1,len(l)): 14 if l[i]>l[j]: 15 l[i],l[j]=l[j],l[i] 16 print l 17 #插入排序 18 def insert(l): 19 for i in range(1,len(l)): 20 s=l[i] 21 j=i 22 23 while j>0 and l[j-1]>s: 24 l[j]=l[j-1] 25 j-=1 26 l[j]=s 27 print l 28 #快速排序 29 def fastsort(l,a,b): 30 if a<b: 31 i,j = a,b 32 x=l[a] 33 while a<b: 34 while a<b and l[b]>=x: 35 b-=1 36 l[a]=l[b] 37 while a<b and l[a]<x: 38 a+=1 39 l[b]=l[a] 40 l[b]=x 41 fastsort(l,i,a-1) 42 fastsort(l,a+1,j) 43 return l 44 buble([2,5,6,3,8,4,7,9]) 45 select([2,5,6,3,8,4,7,9]) 46 insert([2,5,6,3,8,4,7,9]) 47 l=fastsort([6,2,7,5,8,3,9],0,6) 48 print "h",l