#coding=utf8
#选择排序----O(n^2)
#拿第i个跟后面的相比符合条件就交换值,直到i=n-1
data = [5,4,6,7,12,35,46,3,2,8,55,22,0]
length = len(data)
for i in range(length-1):
for k in range(length-i-1):
j = i+k+1
if(data[i]<data[j]):
ret = data[i]
data[i] = data[j]
data[j] = ret
print data
#coding=utf8#冒泡排序---O(n^2)data = [5,4,6,8,9,3,2,1,11,7,23,12]length = len(data)for i in range(length): for j in range(length-1): if(data[j]<data[j+1]): ret = data[j+1] data[j+1] = data[j] data[j] = retprint data
#coding = utf8#归并排序-----O(nlogn)不递归的会算def recur(data): if len(data)<=1: return data p = len(data)/2 left = recur(data[0:p]) right = recur(data[p:]) return merge(left,right)def merge(left,right): l , r = 0,0 result = [] while l<len(left) and r<len(right): if(left[l]<right[r]): result.append(left[l]) l+=1 else: result.append(right[r]) r+=1 result+=left[l:] result+=right[r:] return resultprint recur([4,6,3,8,1,2,9,11,5,22,13,45,15,14,22])