Hoare quick sort. Θ(nlgn)

from random import shuffle

def hoarePartition(array, p, r):
    x = array[p]
    i = p
    j = r
    while True:
        while array[j] > x:
            j -= 1
        while array[i] < x:
            i += 1
        if i < j:
            array[i], array[j] = array[j], array[i]
            i += 1
            j -= 1
        else:
            return j

def quickSort(array, p, r):
    if p < r:
        division = hoarePartition(array, p, r)
        quickSort(array, p, division)
        quickSort(array, division+1, r)


l = list(range(10))
shuffle(l)
print(l)
##############

quickSort(l, 0, len(l)-1)

##############
print(l)

你可能感兴趣的:(Hoare quick sort. Θ(nlgn))