[算法导论]quicksort algorithm @ Python

算法导论上面快速排序的实现。

代码:

def partition(array, left, right):
    i = left-1
    for j in range(left, right):
        if array[j] <= array[right]:
            i += 1
            array[j], array[i] = array[i], array[j]
    array[i+1], array[right] = array[right], array[i+1]
    return i+1

def quicksort(array, left, right):
    if left < right:
        pivot = partition(array, left, right)
        quicksort(array, left, pivot-1)
        quicksort(array, pivot+1, right)


array = [3, 2, 1, 0, 9, 8, 7, 6, 5, 4]
quicksort(array, 0, 9)
print array

 

posted @ 2014-06-03 18:17  南郭子綦  阅读(1296)  评论(0编辑  收藏  举报