标签:插入 min class 交换 for str i+1 max 排序
python排序算法实现(冒泡、选择、插入)
python 从小到大排序 1.冒泡排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)): for j in range((i+1),len(s)): s[i],s[j]=min(s[i],s[j]),max(s[i],s[j]) #print count print s 2.选择排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)): temp = i for j in range(i + 1, len(s)): if s[j] < s[temp]: temp = j if temp != i: count += 1 s[i], s[temp] = s[temp], s[i] print s #print count 3.插入交换: O(n2) s=[3,4,2,5,1,8,0,9] for i in range (len(s)): for j in range(i,0,-1): if s[j] < s[j-1]: s[j],s[j-1]=s[j-1],s[j] else: break print s
标签:插入 min class 交换 for str i+1 max 排序
原文地址:http://www.cnblogs.com/christychang/p/6055659.html