python版的快速排序算法代码
def qsort(L): return ((qsort([x for x in L[1:] if x < L[0]]) + L[0:1] + qsort([x for x in L[1:] if x >= L[0]]) ) if L else [])
版本2
def qsort3(arr, l, r): def swap(arr, s, d): if s != d: tmp = arr[s] arr[s] = arr[d] arr[d] = tmp if l >= r: return m = l for i in range(l, r): if arr[i] <= arr[r]: swap(arr, i, m) m += 1 swap(arr, m, r) qsort3(arr, l, m-1) qsort3(arr, m+1, r) return arr
版本3
def qsort2(L): if len(L)<=1: return L pivot=L[0] less= [x for x in L if x<pivot] equal= [x for x in L if x==pivot] greater= [x for x in L if x>pivot] return qsort2(less)+equal+qsort2(greater)