d****n 发帖数: 1637 | 1 q=lambda x:(lambda o=lambda s:[i for i in x if cmp(i,x[0])==s]:len(x)>1
and
q(o(-1))+o(0)+q(o(1)) or x)( )
>>> q([1,23,1,4,1,23,65,6,78,9,234,10])
[1, 1, 1, 4, 6, 9, 10, 23, 23, 65, 78, 234] | X****r 发帖数: 3557 | 2 There is really no point writing Python code like that.
If you like functional style, just use Haskell:
q s = case s of{[]->[];(x:xs)->q [y|y<-xs,y=x]}
Which does almost (except for the case the the pivot element is repeated)
exactly you do here and is more readable.
Note that this 'quicksort' (both the Python and the Haskell version)
is not really a quicksort, i.e. it does not sort in-place, thus has
quadratic space and time cost.
【在 d****n 的大作中提到】 : q=lambda x:(lambda o=lambda s:[i for i in x if cmp(i,x[0])==s]:len(x)>1 : and : q(o(-1))+o(0)+q(o(1)) or x)( ) : >>> q([1,23,1,4,1,23,65,6,78,9,234,10]) : [1, 1, 1, 4, 6, 9, 10, 23, 23, 65, 78, 234]
| d****n 发帖数: 1637 | 3 haskell like version:
def qsort(L):
if len(L) <= 1: return L
return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1] + \
qsort([ge for ge in L[1:] if ge >= L[0]]) | t*s 发帖数: 1504 | 4 this is not pythonic
the pythonic way is to use built-in sort
【在 d****n 的大作中提到】 : haskell like version: : def qsort(L): : if len(L) <= 1: return L : return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1] + \ : qsort([ge for ge in L[1:] if ge >= L[0]])
| l********a 发帖数: 1154 | 5
+1
【在 t*s 的大作中提到】 : this is not pythonic : the pythonic way is to use built-in sort
|
|