quicksort可以稳定吗?

时间:2016-11-16 19:04:52

标签: algorithm python-2.7 sorting stability

如果在快速排序期间也考虑了与枢轴相等的值,它可以被称为稳定排序吗?这是我对quicksort的实现:

def isOrdered(aList):
ordered = True
for i in range(len(aList)-1):
    if aList[i] > aList[i+1]:
        ordered = False
        break
return ordered

def partition(List,pivotIndex):
    pivot=List[pivotIndex]
    less=[]
    equal=[]
    greater=[]
    if pivotIndex<len(List):
        for i in List:
            if i<pivot:
                less.append(i)
            elif i==pivot:
                equal.append(i)
            else:
                greater.append(i)
    returnlist= [less,equal,greater]
    return returnlist

def quicksort(List,pivotIndex=0):
    sortedList = []
    if pivotIndex>=len(List):
        pivotIndex%=len(List)
    for i in partition(List,pivotIndex):
        for j in i:
            sortedList.append(j)
    pivotIndex+=1
    if isOrdered(sortedList):
        return sortedList
    else:
        return quicksort(sortedList,pivotIndex)

是否有可能同时提高稳定性并保持快速排序的计算速度?

0 个答案:

没有答案