如何改进方案中的这种归并排序?
我是一名 C++ 程序员,我写这段代码是为了看看我是否可以进行函数式思考:) 有什么改进的提示吗?
(define (append listOne listTwo)
(cond
((null? listOne) listTwo)
(else (cons (car listOne) (append (cdr listOne) listTwo)))))
(define (merge listOne listTwo)
(cond
((null? listOne) listTwo)
((null? listTwo) listOne)
((< (car listOne) (car listTwo))
(append (cons (car listOne) '())
(merge (cdr listOne) listTwo)))
((= (car listOne) (car listTwo))
(append (cons (car listOne) '())
(merge (cdr listOne) listTwo)))
(else (append (cons (car listTwo) '())
(merge listOne (cdr listTwo))))))
(define (mergesort lis)
(cond
((null? lis) '())
((null? (cdr lis)) lis)
(else (merge (cons (car lis) '())
(mergesort (cdr lis))))))
(mergesort '(99 77 88 66 44 55 33 11 22 0))
I am a C++ programmer, I wrote this code to see if I can think functionally :)
Any hints to improve it ?
(define (append listOne listTwo)
(cond
((null? listOne) listTwo)
(else (cons (car listOne) (append (cdr listOne) listTwo)))))
(define (merge listOne listTwo)
(cond
((null? listOne) listTwo)
((null? listTwo) listOne)
((< (car listOne) (car listTwo))
(append (cons (car listOne) '())
(merge (cdr listOne) listTwo)))
((= (car listOne) (car listTwo))
(append (cons (car listOne) '())
(merge (cdr listOne) listTwo)))
(else (append (cons (car listTwo) '())
(merge listOne (cdr listTwo))))))
(define (mergesort lis)
(cond
((null? lis) '())
((null? (cdr lis)) lis)
(else (merge (cons (car lis) '())
(mergesort (cdr lis))))))
(mergesort '(99 77 88 66 44 55 33 11 22 0))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到的只有一个小改进:
可以在任何地方简化为
我认为你正在考虑类似的东西(用Python式的语法):
但是cons将一个项目直接添加到列表的前面,就像一个函数
push
,所以就像下面的代码:最终,额外的附加只会导致每个项目的双 con 单元分配,所以这不是什么大问题。
另外,我认为如果您使
mergesort
更加精美,以便它保持列表长度并在每一步对列表的两半进行排序,您可能会获得更好的性能。 不过,这可能不适合这样的学习示例。就像是:
There's only one small improvement that I see:
can everywhere be simplified to
I think you were thinking of something like (in Python-esque syntax):
But cons adds an item directly to the front of a list, like a functional
push
, so it's like the following code:Ultimately the extra append only results in double cons cell allocation for each item, so it's not a big deal.
Also, I think you might get better performance if you made
mergesort
fancier so that it maintains the list length and sorts both halves of the list at each step. This is probably not appropriate for a learning example like this, though.Something like:
一般来说,mergesort 会进行大量的列表操作,因此最好通过“就地”对子部分进行排序来进行破坏性的操作。 您可以查看
sort的实现
是起源于 SLIB 的通用代码的示例。 (乍一看可能有点吓人,但如果您阅读评论并忽略旋钮和优化,您就会在那里看到所有内容。)
此外,您应该查看 SRFI-32 有关排序接口的扩展讨论。
In general,
mergesort
is doing a lot of list manipulations, so it is much better to do things destructively by sorting sub parts "in-place". You can see the implementation ofsort
in PLT Scheme for example of a common code, which originated in SLIB. (It might look intimidating on first sight, but if you read the comments and ignore the knobs and the optimizations, you'll see it all there.)Also, you should look at SRFI-32 for an extended discussion of a sorting interface.