;#########Bubble Sorting################;;
;This function when executed once, the lagest element reaches the end of the list
(define (loop L)
(if (null? (cdr L))
L
(if (< (car L) (cadr L))
(cons (car L) (loop (cdr L)))
(cons (cadr L) (loop (cons (car L) (cddr L))))
)
)
)
;Funtion to return the last value in a list
(define (end-value L)
(cond ((null? L) #f)
((null? (cdr L)) (car L))
(else (end-value (cdr L)))
)
)
;Function to return the list, removing the last element
(define (except-last L)
(cond ((null? L) #f)
((null? (cdr L)) '() )
(else (cons (car L)(except-last (cdr L))))
)
)
;Function add an element to the end of a list
(define (add-to-end L e)
(append L (list e))
)
(define (bubble-sort L)
(if (or (null? L) (null? (cdr L)))
L
(add-to-end (bubble-sort (except-last (loop L)))
(end-value (loop L))
)
)
)Wednesday, February 22, 2012
Recursive Bubble Sorting in Scheme
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment