def quick_sort(alist,first,last):
if first >= last:
return alist
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]
#low右移动
while low <high and alist[low] < mid_value:
low += 1
alist[high] = alist[low]
#从循环退出时,low == high
alist[high] = mid_value
#对quick sort左边执行快速排序
quick_sort(alist,first,low-1)
#对quick sort右边执行快速排序
quick_sort(alist,low+1,last)
if __name__ == "__main__":
alist = [54,26,93,17,77,31,44,55,20]
print(alist)
list=quick_sort(alist,0,len(alist)-1)
print(alist)
结果
[54, 26, 93, 17, 77, 31, 44, 55, 20]
[17, 20, 26, 31, 44, 54, 55, 77, 93]
快速排序算法python实现
最新推荐文章于 2024-04-26 16:26:48 发布