快速排序 python实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20417499/article/details/82052905
#!/usr/bin/python

def quicksort(array):
    if len(array) < 2:
        return array
    else:
        pivot = array[0]
        less = [i for i in array[1:] if i <= pivot]
        print(less)
        greater = [i for i in array[1:] if i > pivot]
        print(greater)

    return quicksort(less) + [pivot] + quicksort(greater)

  

if __name__ == '__main__':
    print(quicksort([10,5,3,2,6]))
    

输出:


[5, 3, 2, 6]# 第一次less
[]#第一次grester
[3, 2]
[6]
[2]
[]
[2, 3, 5, 6, 10] #最终结果

猜你喜欢

转载自blog.csdn.net/qq_20417499/article/details/82052905
今日推荐