希尔算法
a=[8,2,7,5,1,9,6,4,3]
point=1
dk=len(a)
while True:
dk=dk//2
for n in range(dk):
point=dk+n
while point <len(a):
temp=a[point]
index=-1
for i in range(n,point,dk):
if temp < a[i]:
index=i
break;
if index != -1:
for i in reversed(range(index,point,dk)):
a[i+dk]=a[i]
a[index]=temp
point +=dk
if dk ==1:
break
print(a)
冒泡排序
a=[5,2,7,4,3,6,8,0,1,9]
for i in range(len(a)-1):
min=0
for s in range(len(a)-1-i):
if a[min] > a[s+1]:
min=s+1
if a[min] != a[len(a)-1-i]:
temp=a[min]
a[min]=a[len(a)-i-1]
a[len(a)-1-i]=temp
print(a)
- 快速排序(ps:快速排序是对冒泡排序的升级, 快速排序更适合与对随机数的排序)
[python] view plain copy
a=[9,2,7,4,3,6,5,8,1]
def quickScore(a,start,end):
temp=a[start]
for i in range(start+1,end):
if temp > a[i] :
temp2=a[i]
for n in reversed(range(start,i)):
a[n+1]=a[n]
a[start]=temp2
index=a.index(temp)
if index - start > 1 :
quickScore(a,start,index)
if end-1 - index > 1 :
quickScore(a,index+1,end)
quickScore(a,0,9)
print(a)
3.1插入排序–直接插入排序
编写思想以及步骤如下:
if index != -1:
for i in reversed(range(index,point)):
a[i+1]=a[i]
a[index]=point
point+=1
print(a)
[python] view plain copy
a=[9,2,7,4,3,6,5,8,1]
bijiao=0
jiaohuan=0
point=1
while point<len(a):
temp=a[point]
index=-1
for i in range(point):
bijiao+=1
if temp<a[i]:
index=i
break
if index !=-1:
for i in reversed(range(index,point)):
a[i+1]=a[i]
jiaohuan+=1
a[index]=temp
point+=1
print(a)
print('比较次数:'+str(bijiao))
print('交换次数:'+str(jiaohuan))