python实现快速排序
第一种(两个指针分别都两头开始)
#6.快速排序 时间复杂度O(nlogn) 空间复杂度O(logn)
def quick_sort(L,start,end):
if start<end:
i = start
j = end
pivot = L[start]
while i<j:
while L[j]>=pivot and i<j:
j -= 1
L[i], L[j] = L[j], L[i]
while L[i]<=pivot and i<j:
i += 1
L[i],L[j]=L[j],L[i]
quick_sort(L,start,i-1)
quick_sort(L,i+1,end)
return L
L=[5,6,4,8,2,1,7,3,0,1]
print(quick_sort(L,0,len(L)-1))
结果#[0, 1, 1, 2, 3, 4, 5, 6, 7, 8]
第二种方法 算法导论版本(两个指针都从头开始)