Python实现快排

  • Python实现快排
def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quicksort(left) + middle + quicksort(right)
print(quicksort([3, 6, 8, 19, 1, 5]))  # [1, 3, 5, 6, 8, 19]
posted @ 2019-02-24 20:22  YangZhaonan  阅读(2398)  评论(0编辑  收藏  举报