我写一个递归码冒泡排序(从小到大通过交换)
我有一个代码做冒泡排序只是一次泡泡方案
(define (bubble-up L)
(if (null? (cdr L))
L
(if (< (car L) (cadr L))
(cons (car L) (bubble-up (cdr L)))
(cons (cadr L) (bubble-up (cons (car L) (cddr L))))
)
)
如果我把一张表这(8 9 4 2 6 7)) - >'(8 4 2 6 7 9)
现在我是代码,它返回最后一个号码列表
EX试图写一个代码来做(泡泡L)N次(列表中的整数数量)
我有这个公司de:
(define (bubble-sort-aux N L)
(cond ((= N 1) (bubble-up L))
(else (bubble-sort-aux (- N 1) L)
(bubble-up L))))
(bubble-sort-aux 6 (list 8 9 4 2 6 7)) -> ' (8 4 2 6 7 9)
但是递归似乎没有发生,因为它只排序一次!
任何建议将受到欢迎,我只是难住!
“我正在写一个递归代码到Bubble Sort” - 不要! –
@MitchWheat AveryPoole在Scheme中编写,其中tail-call优化是由规范强制的。迭代通常是通过Scheme中的尾递归实现的。 Recation _is_是在Scheme中实现这一点的自然事物。 –
有没有其他方法?刚开始编写代码,尾递归是我学到的唯一方法。 @MitchWheat –