是否可以只通过一次就对列表进行快速排序?
我正在学习haskell,我看到的函数定义是:
quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more)
where less = filter (< x) xs
equal = filter (== x) xs
more = filter (> x) xs
是否可以只用列表的一次遍历而不是3次来编写它?
I am learning haskell and the function definition I see is:
quickSort (x : xs) = (quickSort less) ++ (x : equal) ++ (quickSort more)
where less = filter (< x) xs
equal = filter (== x) xs
more = filter (> x) xs
Is it possible to write it with only one traversal of the list, instead of 3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的意思是这样的吗?
请注意,这并不是真正的快速排序,因为真正的快速排序是就地的。
You mean something like this?
Note that this isn't really quicksort, as the real quicksort is in-place.
它似乎没有改善任何事情,但:
It does not seem to improve anything but:
尽管晚了,但这里的版本应该不会泄漏那么多空间(并且似乎比这里的其他三路版本运行速度大约快两倍):
这解决了使用元组时可能出现的问题,其中< code>let (a,b) = ... 实际上翻译为
let t= ...; a=fst t; b=snd t
这会导致即使在a
被消耗和处理之后,它仍然保持活动状态,作为元组t
的一部分,以便从中读取b
- 尽管当然完全没有必要。这被称为“Wadler 对空间泄漏”问题。或者也许 GHC(带有-O2
)比这更聪明。 :)另外,这显然使用了差异列表方法(谢谢,hammar),这也使其更加高效(大约比使用元组的版本快两倍)。我认为
part
使用累加器参数,因为它以相反的顺序构建它们。Although late, here's a version that's supposed to not leak space as much (and seems to run about twice faster than the other 3-way version here):
This addresses the possible problem with using tuples, where
let (a,b) = ...
is actually translated intolet t= ...; a=fst t; b=snd t
which leads to the situation where even aftera
has been consumed and processed, it is still kept around alive, as part of the tuplet
, forb
to be read from it - though of course completely unnecessary. This is known as "Wadler pair space leak" problem. Or maybe GHC (with-O2
) is smarter than that. :)Also this apparently uses difference lists approach (thanks, hammar) which also makes it a bit more efficient (about twice faster than the version using tuples). I think
part
uses accumulator parameters, as it builds them in reversed order.