标签:return == [] 关于 def sort 源码 turn ret
下面的代码段是关于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)
标签:return == [] 关于 def sort 源码 turn ret
原文地址:http://blog.51cto.com/14117740/2348925