python实现快速排序

废话不说,直接上代码

 

#coding=utf-8


def qSort(alist, first, last):
if first >= last:
return

key = alist[first]
lo = first
hi = last

while lo < hi:
while lo < hi and key <= alist[hi]:
hi -= 1
alist[lo] = alist[hi]

while lo < hi and key > alist[lo]:
lo += 1
alist[hi] = alist[lo]

alist[lo] = key

qSort(alist, first, lo - 1)
qSort(alist, lo + 1, last)

if __name__=="__main__":
bb = [6, 1, 2, 7, 9, 3, 4, 5, 10, 8]
qSort(bb, 0, len(bb) - 1)
print(bb)

运行结果:

F:\dev\python\python.exe F:/pyCharm/pratice/sort_demo/quick_sort.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Process finished with exit code 0

posted @ 2019-03-07 15:47  志不坚者智不达  阅读(158)  评论(0编辑  收藏  举报