python实现快速排序

发布时间:2019-07-15 10:48:02编辑:auto阅读(1755)

    def sortList(alist):
        alen = len(alist)
        if alen == 0:
            return alist
        if alen > 0:
            aitem = alist[alen / 2]
            aleftlist = []
            arightlist = []
            for i in range(0,alen):
                afirstitem = alist[i]
                if i <> alen / 2:
                    if afirstitem > aitem:
                        arightlist.append(afirstitem)
                    else:
                        aleftlist.append(afirstitem)
            aleftlist = sortList(aleftlist)
            arightlist = sortList(arightlist)
            aleftlist.append(aitem)
            return aleftlist + arightlist

    alist = [0,1,2]
    print(sortList(alist))

关键字