2019.9.24-快速排序实现(完整代码)

# coding:utf-8


def quick_sort(alist, first, last):
"""快速排序"""
if first >= last:
return
mid_value = alist[first]
low = first
high = last
while low < high:
# high 左移
while low < high and alist[high] >= mid_value:
high -= 1
alist[low] = alist[high]

while low < high and alist[low] < mid_value:
low += 1
alist[high] = alist[low]
#從循環退出時, low==high
alist[low] = mid_value

# 對low左邊的列表執行快速排序
quick_sort(alist, first, low-1)

# 對low右邊的列表排序
quick_sort(alist, low+1, last)


if __name__ == "__main__":
li = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print(li)
quick_sort(li, 0, len(li)-1)
print(li)

 

 

 

 

 

 

 

执行结果

 

posted @ 2019-09-24 20:41  李俊鹏Python  阅读(371)  评论(0编辑  收藏  举报