如何改进方案中的这种归并排序?

发布于 2024-07-26 02:06:13 字数 867 浏览 5 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的黑色迷你裙 2024-08-02 02:06:13

我看到的只有一个小改进:

(append (cons (car listTwo) '())
              (merge listOne (cdr listTwo))))

可以在任何地方简化为

(cons (car listTwo)
      (merge listOne (cdr listTwo)))

我认为你正在考虑类似的东西(用Python式的语法):

[car(listTwo)] + merge(listOne, cdr(listTwo))

但是cons将一个项目直接添加到列表的前面,就像一个函数push,所以就像下面的代码:

push(car(listTwo), merge(listOne, cdr(listTwo)))

最终,额外的附加只会导致每个项目的双 con 单元分配,所以这不是什么大问题。

另外,我认为如果您使 mergesort 更加精美,以便它保持列表长度并在每一步对列表的两半进行排序,您可能会获得更好的性能。 不过,这可能不适合这样的学习示例。

就像是:

(define (mergesort l)
  (let sort-loop ((l l) (len (length l)))
    (cond
      ((null? l) '())
      ((null? (cdr l)) l)
      (else (merge (sort-loop (take (/ len 2) l) (/ len 2)))
                   (sort-loop (drop (/ len 2) l) (/ len 2)))))))))
(define (take n l)
  (if (= n 0)
      '()
      (cons (car l) (take (sub1 n) (cdr l)))))
(define (drop n l)
  (if (= n 0)
      l
      (drop (sub1 n) (cdr l))))

There's only one small improvement that I see:

(append (cons (car listTwo) '())
              (merge listOne (cdr listTwo))))

can everywhere be simplified to

(cons (car listTwo)
      (merge listOne (cdr listTwo)))

I think you were thinking of something like (in Python-esque syntax):

[car(listTwo)] + merge(listOne, cdr(listTwo))

But cons adds an item directly to the front of a list, like a functional push, so it's like the following code:

push(car(listTwo), merge(listOne, cdr(listTwo)))

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:

(define (mergesort l)
  (let sort-loop ((l l) (len (length l)))
    (cond
      ((null? l) '())
      ((null? (cdr l)) l)
      (else (merge (sort-loop (take (/ len 2) l) (/ len 2)))
                   (sort-loop (drop (/ len 2) l) (/ len 2)))))))))
(define (take n l)
  (if (= n 0)
      '()
      (cons (car l) (take (sub1 n) (cdr l)))))
(define (drop n l)
  (if (= n 0)
      l
      (drop (sub1 n) (cdr l))))
半城柳色半声笛 2024-08-02 02:06:13

一般来说,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 of sort 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文