使用Tukey的Ninther枢轴对现代分区(Lomuto分区)进行快速排序

时间:2019-11-25 15:23:38

标签: python pivot quicksort partition

def med3(A:list, lo:int, hi:int): 
    return st.median([A[0], A[len(A)//2], A[len(A)-1]])
def ninther_index(A:list(), lo:int, hi:int)->int: 
    A_cutted = A[lo : hi+1]
    return A.index(med3([med3(A_cutted[ 0 : len(A_cutted)//3 ]), med3(A_cutted[ len(A_cutted)//3 : 2*len(A_cutted)//3 ]), med3(A_cutted[ 2*len(A_cutted)//3 : len(A_cutted) ])]))

def quicksort(A:list(), lo:int, hi:int):
    if lo < hi:
        p = partition(A, lo, hi)
        quicksort(A, lo, p - 1)
        quicksort(A, p + 1, hi)

def partition(A:list(), lo:int, hi:int) -> int:
    ni = ninther_index(A, lo, hi)
    A[hi], A[ni] = A[ni], A[hi]
    print(A, lo, hi)
    pivot = A[hi]
    i = lo
    for j in range(lo, hi+1):
        if A[j] < pivot:
            A[i], A[j] = A[j], A[i]
            i += 1
    A[i], A[hi] = A[hi], A[i]
    return i

我需要使用带有Tukey的ninther枢轴的Lomuto分区进行快速排序,但这不起作用。我该怎么办才能解决问题?

0 个答案:

没有答案