我正在写一个递归代码到冒泡排序(通过交换从最小到最大)
我有一个代码只进行一次冒泡排序
(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))))
)
)
Run Code Online (Sandbox Code Playgroud)
如果我在这段代码中放入一个列表,它会返回
EX 末尾最大数字的列表..(起泡'(8 9 4 2 6 7)) - >'(8 4 2 6 7 9)
现在我正在尝试编写代码来执行(冒泡L)N次(列表中的整数数)
我有这样的代码:
(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 …Run Code Online (Sandbox Code Playgroud)