Python 排序算法总结

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))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值