解析过程,以后再补充。
import random
def partition(arr, l, r):
x = arr[r]
i = l - 1
j = l
while j <= r - 1:
if arr[j] <= x:
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
j = j + 1
arr[i + 1], arr[r] = arr[r], arr[i + 1]
return i + 1
def quicksort(arr, l, r):
if l < r:
q = partition(arr, l, r)
quicksort(arr, l, q - 1)
quicksort(arr, q + 1, r)
if __name__ == '__main__':
arrr = random.sample(range(1, 10000), 19)
print arrr
quicksort(arrr, 0, len(arrr) - 1)
print arrr