1. 一行程式碼實現的簡潔版本

``````quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in array[1:] if item <= array[0]])   [array[0]]   quick_sort([item for item in array[1:] if item > array[0]])
``````

2. 網上常見的快排實現

``````def quick_sort(array, left, right):
if left >= right:
return
low = left
high = right
key = array[low]
while left < right:
while left < right and array[right] > key:
right -= 1
array[left] = array[right]
while left < right and array[left] <= key:
left  = 1
array[right] = array[left]
array[right] = key
quick_sort(array, low, left - 1)
quick_sort(array, left   1, high)``````

array如果是個列表的話，可以通過len(array)求得長度，但是後邊遞迴呼叫的時候必須使用分片，而分片執行的原列表的複製操作，這樣就達不到原地排序的目的了，所以還是要傳上邊界和下邊界的。

3.《演算法導論》中的快排程式

``````def quick_sort(array, l, r):
if l < r:
q = partition(array, l, r)
quick_sort(array, l, q - 1)
quick_sort(array, q   1, r)
def partition(array, l, r):
x = array[r]
i = l - 1
for j in range(l, r):
if array[j] <= x:
i  = 1
array[i], array[j] = array[j], array[i]
array[i   1], array[r] = array[r], array[i 1]
return i   1``````

4. 用棧實現非遞迴的快排程式

1）棧裡邊儲存什麼？

2）迭代結束的條件是什麼？

``````def quick_sort(array, l, r):
if l >= r:
return
stack = []
stack.append(l)
stack.append(r)
while stack:
low = stack.pop(0)
high = stack.pop(0)
if high - low <= 0:
continue
x = array[high]
i = low - 1
for j in range(low, high):
if array[j] <= x:
i  = 1
array[i], array[j] = array[j], array[i]
array[i   1], array[high] = array[high], array[i   1]
stack.extend([low, i, i   2, high])``````