基于交换的方式,减少了原来快排的L,G,pivot等内存使用
"""
Data: 2019/6/24 14:42
"""
def inplace_quick_sort(S, a, b):
if a >= b: return
pivot = S[b]
left = a
right = b-1
while left <= right:
while left <= right and S[left] < pivot:
left += 1
while left <= right and pivot < S[right]:
right -= 1
if left <= right:
S[left], S[right] = S[right], S[left]
left, right = left+1, right-1
S[left], S[b] = S[b], S[left]
inplace_quick_sort(S, a, left-1)
inplace_quick_sort(S, left+1, b)
return S
S = [1, 8, 6, 5, 7, 2]
print(inplace_quick_sort(S, 0, 5))