Sorting bubbles in a pattern

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!

+4
source share
2

:

(define (bubble-sort-aux N L)   
  (cond ((= N 1) (bubble-up L))  
        (else (bubble-sort-aux (- N 1) (bubble-up L)))))  

"" N , . , bubble-up -, , bubble-up, , . , :

(bubble-sort-aux 6 (list 8 9 4 2 6 7))
=> '(2 4 6 7 8 9)
+5

:

(define (bubble-swap ls)
  (if (null? (cdr ls))
      ls
      (if (> (car ls) (cadr ls))
          (cons (cadr ls) (bubble-swap (cons (car ls) (cddr ls))))
          (cons (car ls) (bubble-swap (cdr ls))))))

(define (len ls)
  (if (null? ls)
      0
      (+ 1 (len (cdr ls)))))

(define (bubblesort_ ls n)
  (if (= n 1)
      ls
      (bubblesort_ (bubble-swap ls) (- n 1))))

(define (bubblesort ls) (bubblesort_ ls (len ls)))

len, length, .

0

All Articles