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
原文地址:https://www.cnblogs.com/zhzhang/p/8595351.html