Python快速排序的实现

代码:
from random import randint
def quick_sort(lst,first,last):
low=first
high=last
if first<last:
mid_value=lst[first]
while low<high:
while low<high and lst[high]>=mid_value:
high=high-1
lst[low]=lst[high]
while low<high and lst[low]<=mid_value:
low=low+1
lst[high]=lst[low]
lst[low]=mid_value
quick_sort(lst,first,low-1)
quick_sort(lst,low+1,last)
return lst
l=[]
for i in range(10):
l.append(randint(1,100))
print(l)
print(l,0,len(l-1))
运行结果:

[89, 86, 5, 76, 12, 13, 20, 57, 20, 18]
[5, 12, 13, 18, 20, 20, 57, 76, 86, 89]

猜你喜欢

转载自www.cnblogs.com/sunflowers-lanqijiu/p/11681134.html