python 快速排序

"""
说明:
先从array取一个数作为中间值赋值给mid,然后遍历所有元素,把大于mid的值放到greater,把小于等于mid的值放到less,最后再递归对less和greater进行快速排序
"""
def quick_sort(array):
    less = []
    greater = []
    if len(array) <= 1:
        return array
    mid = array.pop()
    for x in array:
        if x <= mid:
            less.append(x)
        else:
            greater.append(x)
    return quick_sort(less) + [mid] + quick_sort(greater)


if __name__ == '__main__':
    a = quick_sort([9, 33, 4, 5, 77, 89, 24])
    print(a)

你可能感兴趣的:(数据结构和算法)