import math
def BubbleSort(aList):
count = [i for i in range(len(aList))]
for i in count[::-1]:
flag = 0
for j in range(0,i):
if aList[j] > aList[j+1]:
aList[j],aList[j+1] = aList[j+1],aList[j]
flag = 1
if flag == 0:
break
return aList
def InsertSort(aList):
for i in range(1,len(aList)):
temp = aList[i]
while i>0 and aList[i-1]> temp:
aList[i] = aList[i-1]
i -= 1
aList[i] = temp
return aList
def ShellSort(aList):
D = len(aList)//2
while D>0:
for i in range(D,len(aList),D):
temp = aList[i]
while i>=D and aList[i-D] > temp:
aList[i] = aList[i-D]
i -= D
aList[i] = temp
D //= 2
return aList
def createHeap(aList,root,size):
if 2*root+1 < size:
k = 2*root + 2 if 2*root+2 <size and aList[2*root+2] > aList[2*root+1] else 2*root+1
if aList[k] > aList[root]:
aList[root],aList[k] = aList[k],aList[root]
createHeap(aList,k,size)
def Merge(aList,bList):
i = j = 0
c = []
while(i<len(aList) and j<len(bList)):
if(aList[i]<=bList[j]):
c.append(aList[i])
i += 1
elif (aList[i]>bList[j]):
c.append(bList[j])
j += 1
for d in range(j,len(bList)):
c.append(bList[d])
for e in range(i,len(aList)):
c.append(aList[e])
return c
def MergeSort(aList):
if len(aList) < 2:
return aList
middle = len(aList)//2
c = MergeSort(aList[:middle])
d = MergeSort(aList[middle:])
return Merge(c,d)
def MergeSortF(aList,length):
i = 0
while(i<len(aList)-2*length):
c = Merge(aList[i:i+length],aList[i+length:i+2*length])
aList[i:i+2*length] = c
i += 2*length
if(i+length<len(aList)):
c = Merge(aList[i:i+length],aList[i+length:])
aList[i:] = c
def QuickSort(aList,left,right):
i = left
j = right
if left < right:
pivot = aList[left]
while left < right:
while left<right and aList[right] >= pivot:
right -= 1
aList[left] = aList[right]
while left < right and aList[left] <= pivot:
left += 1
aList[right] = aList[left]
aList[left] = pivot
QuickSort(aList,i,left)
QuickSort(aList,left+1,j)
return aList
if __name__ == '__main__':
aList = [1,2, 37, 23, -4,18,1,0,-6,-5,2,3, 7, 23, 2, 8, 42,18]
#print(BubbleSort(aList))
#print(InsertSort(aList))
#print(ShellSort(aList))
# 堆排序
#########
# size = len(aList)
# for i in range(len(aList)//2-1,-1,-1):
# createHeap(aList,i,size)
# for i in range(len(aList)-1,0,-1):
# aList[i],aList[0] = aList[0],aList[i]
# createHeap(aList,0,i)
# print(aList)
#########递归形式
#print(MergeSort(aList))
#
################归并排序非递归形式
# for i in range(0,math.ceil(math.log(len(aList),2))):
# MergeSortF(aList,2**i)
# print(aList)
print(QuickSort(aList,0,len(aList)-1))