首页 > 编程语言 > 详细

算法——快速排序

时间:2021-06-28 15:42:33      阅读:26      评论:0      收藏:0      [点我收藏+]

快速排序

# 快速排序

def main(ary):

    # D&C思想:基线条件
    if len(ary) < 2:
        return ary

    else:
        base = ary[0]

        left = [i for i in ary[1:] if i <= base]
        right = [i for i in ary[1:] if i > base]

        return main(left) + [base] + main(right)


ary = [1, 34, 22, 54, 66, 22, 12, 25, 78, 91, 41, 1, 34, 22, 54, 66, 22, 12, 25, 78, 91, 41, 1, 34, 22, 54, 66, 22, 12, 25, 78, 91, 41]
res = main(ary)
print(res)
"""
[1, 1, 1, 12, 12, 12, 22, 22, 22, 22, 22, 22, 25, 25, 25, 34, 34, 34, 41, 41, 41, 54, 54, 54, 66, 66, 66, 78, 78, 78, 91, 91, 91]
"""




    

算法——快速排序

原文:https://www.cnblogs.com/pywjh/p/14943398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!