def quickSort(alist):
quickSortHelper(alist, 0, len(alist)-1)
def quickSortHelper(alist, first, last):
if first < last:
splitpoint = partition(alist, first, last)
quickSortHelper(alist, first, splitpoint-1)
quickSortHelper(alist, splitpoint+1, last)
def partition(alist, first, last):
pivotvalue = alist[first]
leftmark = first + 1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and \
alist[leftmark] <= pivotvalue:
leftmark += 1
while alist[rightmark] >= pivotvalue and \
rightmark >= leftmark:
rightmark -= 1
if rightmark < leftmark:
done = True
else:
alist[leftmark], alist[rightmark] = alist[rightmark], alist[leftmark]
alist[first], alist[rightmark] = alist[rightmark], alist[first]
return rightmark
alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
quickSort(alist)
print(alist)
清晰的解法:
# quick sort
def quickSort(L, low, high):
i = low
j = high
if i >= j:
return
key = L[i]
while i < j:
while i < j and L[j] >= key: # 从后向前找比key小的值
j -= 1
L[i] = L[j] # 把比key小的值移到key在的位置
while i < j and L[i] <= key: # 从前向后找比key大的值
i += 1
L[j] = L[i] # 把比key大的值移到刚找出的比key小的值的位置
L[i] = key # 这时的i左右分别为比key小和大的值,key移到i处
quickSort(L, low, i-1) # 继续排前一部分
quickSort(L, j+1, high) # 继续排后一部分
return
测试用例
a = [4, 5, 3, 12, 43, 1, 0, 34]
quickSort(a, 0, len(a) - 1)
print(a)
结果
[0, 1, 3, 4, 5, 12, 34, 43]
最好和最坏情况
最好:key每次都能使序列均匀划分,时间复杂度为O(nlogn)
最坏:key为最大或最小数字,时间复杂度为O(n^2)