I write recursive code for Bubble Sort (from smallest to largest by replacement)
I have code to make the bubble look only once
(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))))
)
)
if I put the list in this code, it returns the list with the largest number at the end of
EX .. (bubble-up) (8 9 4 2 6 7)) → '(8 4 2 6 7 9)
Now I'm trying to write code to execute (bubble-up L) N times (the number of integers in the list)
I have this code:
(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)
But recursion does not seem to happen, because it only sorts once!
Any suggestions are welcome, I'm just at a standstill!
source
share