;#######Exchange Selection Sort#################;;;;
;Function to find the smallest element in a list
(define (smallest L)
(if (null? (cadr L))
(car L)
(smallest2 L (car L))
)
)
(define (smallest2 L e)
(if (null? L)
e
(if (< (car L) e)
(smallest2 (cdr L) (car L))
(smallest2 (cdr L) e)
)
)
)
;Function to swap an element 'e' in the list with 'new-e' if 'e' is there in the list
(define (swap L e new-e)
(if (null? L)
L
(if (= e (car L))
(cons new-e (cdr L))
(cons (car L) (swap (cdr L) e new-e))
)
)
)
(define (exchange-sort L)
(if (or (null? L) (null? (cdr L)))
L
(cons (smallest L) (exchange-sort (swap (cdr L) (smallest L) (car L))))
)
)Wednesday, February 22, 2012
Recursive Exchange Selection Sort in Scheme
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment