zoukankan      html  css  js  c++  java
  • 快排

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    def quick(list):
        if len(list) < 2:
            return list
     
        tmp = list[0]  # 临时变量 可以取随机值
        left = [x for x in list[1:] if x <= tmp]  # 左列表
        right = [x for x in list[1:] if x > tmp]  # 右列表
        return quick(left) + [tmp] + quick(right)
     
    li = [4,3,7,5,8,2]
    print quick(li)  # [2, 3, 4, 5, 7, 8]
     
    #### 对[4,3,7,5,8,2]排序
    '''
    [3, 2] + [4] + [7, 5, 8]                 # tmp = [4]
    [2] + [3] + [4] + [7, 5, 8]              # tmp = [3] 此时对[3, 2]这个列表进行排序
    [2] + [3] + [4] + [5] + [7] + [8]        # tmp = [7] 此时对[7, 5, 8]这个列表进行排序
    '''

    注:快排代码实现(类似于二叉树 递归调用)----右手左手一个慢动作,右手左手一个慢动作重播

  • 相关阅读:
    Python之路,day16-Python基础
    Python之路,day15-Python基础
    Python之路,day14-Python基础
    django之model操作
    django信号
    django缓存
    CSRF的攻击与防范
    cookie和session
    http协议之Request Headers
    ajax参数详解
  • 原文地址:https://www.cnblogs.com/ngngng/p/13880925.html
Copyright © 2011-2022 走看看