快速排序-理论学习用
快速排序是python list的内部函数,但是为了学习快排理论,所以写出该部分的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)

浙公网安备 33010602011771号