快速排序(python版)

#!coding:utf8

def quicksort(list_num, left, right):
    if left > right:
        return

    low = left
    high = right
    privot= list_num[left]
    while low<high:
        while low < high and list_num[high] > privot:
            high-=1
        if low < high:
            list_num[low] = list_num[high]

        while low < high and list_num[low] <= privot:
            low+=1
        if low < high:
             list_num[high] = list_num[low]

    list_num[low] = privot
    quicksort(list_num, left, low-1)
    quicksort(list_num, low+1, right)

if __name__ == "__main__":
    test = [7,2,1,6,5,4,4,2]
    quicksort(test,0, len(test)-1)

后记:后面可以对privot游标的选择方法进行优化,如:三元中位法(每次选取三个位置(前,中,后)三个数的中位数的下标的值作为privot)

posted @ 2016-02-21 16:26  Esapinit  阅读(324)  评论(0编辑  收藏  举报