快速排序是python list的內部函數,可是爲了學習快排理論,因此寫出該部分的python代碼。python
import random arr = [ 7,6,5,4,3,2,1] def quicksort(L): if len(L) > 1: pivot = random.randrange(len(L)) elements = L[:pivot]+L[pivot+1:] left = [element for element in elements if element < L[pivot]] right =[element for element in elements if element >= L[pivot]] return quicksort(left)+[L[pivot]]+quicksort(right) return L print quicksort(arr)