冒泡排序:
将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮
#!/bin/env python
#_*_ coding:utf-8 _*_
#算法之冒泡排序
li = [13,22,6,99,11]
for m in range(len(li)-1):
if li[m] > li[m+1]:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
#第一次# 13>22 —>pass
#第二次# 22>6 ->li = [13,6,22,99,11]
#第三次# 22>99 ->pass
#第四次# 99>11 ->li = [13,6,22,11,99]
#最大的数字已经找到99,并放在最后,接下来只需要比较前4个数字即可
for m in range(len(li)-2):
if li[m] > li[m+1]:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
#第一次# 13>6 ->li = [6,13,22,11,99]
#第二次# 13>22 ->pass
#第三次# 22>11 ->li = [6,13,11,22,99]
#前4个数字最大数找到22,放在最后,接下来只需比较前3个数字即可
for m in range(len(li)-3):
if li[m] > li[m+1]:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
#第一次# 6>13 ->pass
#第二次# 13>11 ->li = [6,11,13,22,99]
#前3个数字最大数已找到13,放在最后,接来下只需要比较前2个数字即可
for m in range(len(li)-4):
if li[m] > li[m+1]:
temp = li[m]
li[m] = li[m+1]
li[m+1] = temp
#第一次# 6>11 ->pass
#前2个数字最大数为11,放在最后
print li
#---------------->最终结果为: li=[6,11,13,22,99]
############简化版#############################################################
list = [13,22,6,99,11]
for n in range(1,len(list)-1): #n=1,2,3,4 #第一次大循环,n=1,m=4
for m in range(len(list)-n): #第一次小循环m[0]>m[1],第二次m[1]>m[2],第三次[m2]>[m3],第四次[m3>m4]
if list[m] > list[m+1]: #第二次大循环,n=2,m=3
temp = list[m] #第一次小循环m[0]>m[1],第二次m[1]>m[2],第三次[m2]>[m3]
list[m] = list[m+1] #第三次大循环,n=3,m=2
list[m+1] = temp #第一次小循环m[0]>m[1],第二次m[1]>m[2]
#第四次大循环,n=4,m=1
#第一次小循环m[0]>m[1]
print list
#---------------->最终结果为: list=[6,11,13,22,99]
选择排序
先选择中间值,然后把比它小的放在左边,大的放在右边(具体的实现是从两边找,找到一对后交换)。然后对两边分别使用这个过程(递归)
#!/bin/env python
#_*_ coding:utf-8 _*_
import time
import random
#生产随机列表
def get_randow():
list = []
for x in range(100):
i = random.randrange(1000)
list.append(i)
return list
def select_sort(list):
for i in range(len(list)): #for i in range(5) #i=0,1,2,3,4
for j in range(i,len(list)): #for j in range(0,5) #i=0,j=0,1,2,3,4
if list[i] > list[j]: #if 13>13 ->pass
tmp = list[i] #if 13>22 ->pass
list[i] = list[j] #if 13>6 ->list=[6,22,13,99,11]
list[j] = tmp #if 6>99 ->pass
#if 6>11 ->pass
if __name__ == '__main__': #for j in range(1,5) #i=1,j=1,2,3,4
list = [13,22,6,99,11] #if 22>22 ->pass
#list = get_randow() #if 22>13 ->list=[6,13,22,99,11]
start_time = time.time() #if 13>99 ->pass
select_sort(list) #if 13>11 ->list=[6,11,22,99,13]
end_time = time.time() #for j in range(2,5) #i=2,j=2,3,4
print list #if 22>22 ->pass
print '本次耗时:%s'%(end_time-start_time) #if 22>99 ->pass
#if 22>13 ->list=[6,11,13,99,22]
#for j in range(3,5) #i=3,j=3,4
#if 99>99 ->pass
#if 99>22 ->list=[6,11,13,22,99]
#for j in range(3,5) #i=4,j=4
#if 99>99 ->pass
选择排序-优化版,速度可提高一倍
def select_sort2(list): #list = [13,22,6,99,11]
for i in range(len(list)): #for i in range(5) #i=0,1,2,3,4
smallest_index = i #index=0,1,2,3,4
for j in range(i,len(list)): #for j in range(0,5) #i=0,j=0,1,2,3,4 ,index=0
if list[smallest_index] > list[j]: #if 13>13 ->pass
smallest_index = j #if 13>22 ->pass
tmp = list[i] #if 13>6 ->index=2
list[i] = list[smallest_index] #if 6>99 ->pass
list[smallest_index] = tmp #if 6>11 ->pass
#-> list=[6,22,13,99,11]
#for j in range(1,5) #i=1,j=1,2,3,4 ,index=1
#if 22>22 ->pass
#if 22>13 ->index=2
#if 13>99 ->pass
#if 13>11 ->index=4
#-> list=[6,11,13,99,22]
#for j in range(2,5) #i=2,j=2,3,4 ,index=2
#if 13>13 ->pass
#if 13>99 ->pass
#if 13>22 ->pass
#-> list=[6,11,13,99,22]
#for j in range(3,5) #i=3,j=3,4 ,index=3
#if 99>99 ->pass
#if 99>22 ->index=4
#-> list=[6,11,13,22,99]
#for j in range(4,5) #i=4,j=4 ,index=4
#if 99>99 ->pass
#-> list=[6,11,13,22,99]
插入排序(Insertion Sort)
插入排序(Insertion Sort)的基本思想是:将列表分为2部分,左边为排序好的部分,右边为未排序的部分,循环整个列表,每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子序列中的适当位置,直到全部记录插入完成为止。
#!/bin/env python
#_*_ coding:utf-8 _*_
import time
def insert_sort(list):
for i in range(1,len(list)):
position = i #刚开始往左边走的第一个位置
current_val = list[i] #先把当前值存下来
while position >0 and current_val < list[position-1]:
list[position] = list[position-1]
position -=1
list[position] = current_val
#执行流程
#for i in range(10),i=1,9
#i=1,position=1,current_val=77
#while# 1>0 and 77<92: ->list=[92, 92, 67, 8, 6, 84, 55, 85, 43, 67]
# ->position=0
#while# 0>0 ->pass
#->list=[77, 92, 67, 8, 6, 84, 55, 85, 43, 67]
#i=2,position=2,current_val=67
#while# 2>0 and 67<92: ->list=[77, 92, 92, 8, 6, 84, 55, 85, 43, 67]
# ->postion=1
#while# 1>0 and 67<77: ->list=[77, 77, 92, 8, 6, 84, 55, 85, 43, 67]
# ->postion=0
#while# 0>0 ->pass
#->list=[67, 77, 92, 8, 6, 84, 55, 85, 43, 67]
#i=3,position=3,current_val=8
#while# 3>0 and 8<92: ->list=[67, 77, 92, 92, 6, 84, 55, 85, 43, 67]
# ->postion=2
#while# 2>0 and 8<77: ->list=[67, 77, 77, 92, 6, 84, 55, 85, 43, 67]
# ->postion=1
#while# 1>0 and 8<67: ->list=[67, 67, 77, 92, 6, 84, 55, 85, 43, 67]
# ->postion=0
#while# 0>0 ->pass
#->list=[8, 67, 77, 92, 6, 84, 55, 85, 43, 67]
#..........
if __name__ == '__main__':
list = [92, 77, 67, 8, 6, 84, 55, 85, 43, 67]
start_time = time.time()
insert_sort(list)
end_time = time.time()
print list