0
;; sort : list-of-numbers  ->  list-of-numbers (sorted)
;; to create a list of numbers with the same numbers as
;; alon sorted in descending order
(define (sorts alon)
  (cond
    [(empty? alon) empty]
    [(cons? alon) (insert (first alon) (sorts (rest alon)))]))

(check-expect (sorts (list 3 5 6 7)) (list 7 6 5 3))

如果输入的长度低于某个阈值,则使用上述方法开发一个快速排序版本的练习题

我不太清楚这个问题或他们想要的输出是什么。有什么帮助吗?

4

1 回答 1

1

真的没有什么可做的了。如果我理解正确,您应该可以选择按两种不同的算法进行排序:如果输入列表的长度低于某个数字,则使用quicksort,否则使用sorts您在上面定义的过程。无论哪种方式,预期的输出都是一个排序的数字列表:

(define (my-sort alon threshold)
  (if (< (length alon) threshold)
      (quicksort alon)
      (sorts alon)))

在您所说的问题中:

使用上面开发一个版本的快速排序

虽然可以用(只是为了它见鬼)来写,但它相当没有意义,因为无论如何你必须在能够使用之前对列表进行分区(例如,使用),这最终会调用(在第二)的条件:quicksortinsertfilterinsertconsinsert

(define (quicksort alon)
  (if (empty? alon)
      empty
      (append (quicksort (filter (lambda (x)
                                   (< x (first alon)))
                                 (rest alon)))
              (insert (first alon)
                      (quicksort (filter (lambda (x)
                                           (>= x (first alon)))
                                         (rest alon)))))))
于 2013-11-09T16:15:11.353 回答