利用快速排序来实现一个由小到大的数组排序,快速排序就是利用分治法的思想来分块
处理
def quick_sort(a,low,high):
if low >= high:
return a
i = low
j = high
#定义基准,基准左边小于基数,右边大于基数
pivot = a[low]
while i < j:
#从后向前扫描
while i < j and a[j] > pivot:
j -= 1
a[i] = a[j]
#从前向后扫描
while i < j and a[i] < pivot:
i += 1
a[j] = a[i]
a[j] = pivot
#分段排序
quick_sort(a,low,j-1)
quick_sort(a,j+1,high)
return a
lists=[5,4,3,2,1]
print(quick_sort(lists,0,len(lists)-1))
[1, 2, 3, 4, 5]