对列表进行排序的方案

发布于 2024-08-26 22:38:27 字数 454 浏览 6 评论 0原文

好吧,我正在尝试获取一个列表并将其从最大到最小排序。

Example:
> (maxheap (list 5 6 2 1 18 7))
;output:
> (18 7 6 5 2 1)

到目前为止,这就是我得到的结果:

(define (mkmaxheap heaplist)
  (let ((max (mymax(heaplist))))
  ;mymax is a func that returns max number, it works

    (let (( head (car heaplist)) (tail (cdr heaplist)))
      (if (null? tail)
         newlist))))

这就是我能编译的所有内容,我编写的所有其他代码都失败了。任何有关解决此问题的帮助将不胜感激。

Okay so I am trying to take in a list and sort it from greatest to smallest.

Example:
> (maxheap (list 5 6 2 1 18 7))
;output:
> (18 7 6 5 2 1)

So here's what I got so far:

(define (mkmaxheap heaplist)
  (let ((max (mymax(heaplist))))
  ;mymax is a func that returns max number, it works

    (let (( head (car heaplist)) (tail (cdr heaplist)))
      (if (null? tail)
         newlist))))

Thats all I could get to compile, all the other code I wrote failed. Any help on solving this would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

无畏 2024-09-02 22:38:27

您应该仔细阐明要用于生成排序列表的策略。是这样的吗?

  • 找出列表中的最大数字。
  • 获取列表中除最大值之外的其余部分。
  • 对列表的其余部分进行排序,并将最大值放在前面。

这不是一种非常快的排序方法,但它应该有效。代码的下一步将是编写一个函数来获取列表中除最大值之外的其余部分(如果列表有重复项,请注意正确处理它。)

一旦编写完成,您应该能够编写Scheme代码看起来或多或少类似于上面的轮廓。

You should articulate carefully the strategy that you want to use for producing the sorted list. Is it something like this?

  • Find the maximum number in the list.
  • Get the rest of the list except for the maximum.
  • Sort the rest of the list, and put the maximum on the front of it.

This is not a very fast way to sort it, but it should work. The next step from your code will be to write a function to get the rest of the list except for the maximum (take care to handle it correctly if the list has duplicates.)

Once you have that written, you should be able to write Scheme code that looks more or less just like the outline above.

愁以何悠 2024-09-02 22:38:27

这是 Common Lisp 中的合并排序算法。它大致接近于在方案中实现相同的排序。

(defun merge-sort( input )
  (labels ((right-half ( input )
             (last input (ceiling (/ (length input) 2))))
           (left-half ( input )
             (ldiff input (right-half input ))))
    (if (or (null input) (null (cdr input))) 
        input 
        (merge 'list (merge-sort (left-half input)) (merge-sort (right-half input)) #'<))))

This is a merge sort algorithm in Common lisp. It's roughly close to implementing the same sort in scheme.

(defun merge-sort( input )
  (labels ((right-half ( input )
             (last input (ceiling (/ (length input) 2))))
           (left-half ( input )
             (ldiff input (right-half input ))))
    (if (or (null input) (null (cdr input))) 
        input 
        (merge 'list (merge-sort (left-half input)) (merge-sort (right-half input)) #'<))))
一指流沙 2024-09-02 22:38:27

您需要决定使用什么来对列表进行排序。我最近一直在修改方案,通过 SICP 和“Schemer”系列,我发现在方案中实现冒泡排序、合并排序和快速排序非常容易。

You need to decide what to use to sort the list. I've been tinkering about with scheme recently, working my way through SICP and the "Schemer" series, and I found it pretty easy to implement a bubble sort, merge sort, and quicksort in scheme.

债姬 2024-09-02 22:38:27

您没有指定您正在使用的实现。但它可以实现 r6rs list-排序srfi-95排序或任何其他内置- 排序中。查看您的实施文档。

You didn't specify the implementation you're using. But it may implement either r6rs list-sort or srfi-95 sort or any other built-in sorting. Check out your implementation's documentation.

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