python排序算法实现(冒泡、选择、插入)

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
上一篇:配置Windows 2008 R2 64位 Odoo 8.0 源码PyCharm开发调试环境


下一篇:php七牛批量删除空间内的所有文件方法