1 Star 0 Fork 0

陈焰 / algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
quicksort.py 772 Bytes
一键复制 编辑 原始数据 按行查看 历史
陈焰 提交于 2015-05-09 22:39 . 修改两元素交换时的代码
#coding:utf-8
'''
email: chenyan@feling.net
快速排序(原地排序,归并没办法原地,只能返回新的序列)
invented by Tony Hoare 1962
'''
def partition(l, begin, end):
x = l[begin]
i = begin
for j in range(begin+1, end):
if l[j]<=x:
i += 1
l[i], l[j] = l[j], l[i]
l[i], l[begin] = l[begin], l[i]
return i
def quicksort(l, begin=0, end=-9):
'''
>>> l = [0, 3, 2, 2, 5, 8, 2, 1, 4, 6, 7]
>>> quicksort(l)
>>> l
[0, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8]
'''
if end==-9:
end = len(l)
if begin<end:
i = partition(l, begin, end)
quicksort(l, begin, i)
quicksort(l, i+1, end)
if __name__=='__main__':
import doctest
doctest.testmod()
Python
1
https://gitee.com/chenyanclyz/algorithm.git
git@gitee.com:chenyanclyz/algorithm.git
chenyanclyz
algorithm
algorithm
master

搜索帮助