0

如果在快速排序期间也考虑等于枢轴的值,是否可以称为稳定排序?这是我的快速排序实现:

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)

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

4

0 回答 0