由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 我也来一个, quick sort 只要一行。
相关主题
两行quicksort,不难些吧感觉用python的人,属于程序员中的文艺青年那类的
阅读scala中nv的显卡能战胜intel的CPU么
Python is easy and not easyPython擂台:算24点
算法之极弱问关于FP
哪位大侠给说说 何时用 merge sort, 何时用 quick sort, 何时抛砖引玉,来谈谈functional programming
underlying sort algorithm for SET in STL?粉FP的人是因为把电脑想象成图灵机了
靠。Sedgewick这3w-qsort算法居然还有bug!Scala有一点不好
嵌入式系统用什么sorting算法比较好?Scala又被鄙视了
相关话题的讨论汇总
话题: lambda话题: 23话题: 234话题: sort话题: qsort
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
Scala又被鄙视了哪位大侠给说说 何时用 merge sort, 何时用 quick sort, 何时
这么说吧,fp不是否定变量,而是控制变量的范围underlying sort algorithm for SET in STL?
C++11的lambda不会破坏可读性吗?靠。Sedgewick这3w-qsort算法居然还有bug!
写code都推一起很牛逼吗嵌入式系统用什么sorting算法比较好?
两行quicksort,不难些吧感觉用python的人,属于程序员中的文艺青年那类的
阅读scala中nv的显卡能战胜intel的CPU么
Python is easy and not easyPython擂台:算24点
算法之极弱问关于FP
相关话题的讨论汇总
话题: lambda话题: 23话题: 234话题: sort话题: qsort