zoukankan      html  css  js  c++  java
  • Python实现快速排序

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/3/18 13:59
    # @Author  : baoshan
    # @Site    : 
    # @File    : quickSort.py
    # @Software: PyCharm Community Edition
    
    
    # 快速排序
    def quickSort(l, low, high):
        L = l
        i = low
        j = high
        if i >= j:
            return L
        key = L[i]
        while i < j:
            while i < j and L[j] >= key:
                j = j - 1
            L[i] = L[j]
            while i < j and L[i] <= key:
                i = i + 1
            L[j] = L[i]
        L[i] = key
        quickSort(L, low, i-1)
        quickSort(L, j+1, high)
        return L
    
    
    l = [5, 6, 1, 8, 7, 3, 2, 4]
    print('---原始序列---')
    print(l)
    lresult = quickSort(l, 0, len(l)-1)
    print('---排序后序列---')
    print(lresult)

    输出结果:

    /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/PycharmProjects/myProject/python_weixin_study/quickSort.py
    ---原始序列---
    [5, 6, 1, 8, 7, 3, 2, 4]
    ---排序后序列---
    [1, 2, 3, 4, 5, 6, 7, 8]
    
    Process finished with exit code 0
  • 相关阅读:
    用ruby抓取网页
    [转] 从项目管理角度看敏捷开发
    ruby学习笔记(9)
    [转] 从项目管理角度看敏捷开发
    ruby学习笔记(8)
    ruby学习笔记(7)
    [转] 什么是敏捷开发
    netbeans tomcat
    maven
    jersey
  • 原文地址:https://www.cnblogs.com/zhzhang/p/8595351.html
Copyright © 2011-2022 走看看