This is a bubblesort algorithm written in Scheme. It sorts a list of integers from least to greatest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (re) | |
(load "bubblesort-int-list.scm")) | |
(define (sort-list origlist) | |
(define (get-first a b) | |
(if (< a b) | |
a | |
b)) | |
(define (get-second a b) | |
(if (< a b) | |
b | |
a)) | |
(define (swap-check a b) | |
(not (< a b))) | |
(define (cut-list origlist start end) | |
(define (cutl-help list pos) | |
(if (> pos end) | |
(reverse list) | |
(cutl-help (cons (list-ref origlist (- pos 1)) list) (+ pos 1)))) | |
(cutl-help () start)) | |
(define (slis-help templist position error?) | |
(newline) | |
(display+ templist position error?) | |
(let ((dimlist (length origlist))) | |
(if (and (not error?) (= position dimlist)) | |
templist | |
(if (= position dimlist) | |
(slis-help templist 1 #f) | |
(cond | |
((= position 1) | |
(display+ "Cond 1") | |
(slis-help | |
(append | |
(list | |
(get-first (list-ref templist 0) (list-ref templist 1)) | |
(get-second (list-ref templist 0) (list-ref templist 1))) | |
(cut-list templist 3 dimlist)) | |
(+ position 1) | |
(or (swap-check (list-ref templist 0) (list-ref templist 1)) error?))) | |
((= position (- dimlist 1)) | |
(display+ "Cond 2") | |
(slis-help | |
(append | |
(cut-list templist 1 (- position 1)) | |
(list | |
(get-first (list-ref templist (- dimlist 2)) (list-ref templist (- dimlist 1))) | |
(get-second (list-ref templist (- dimlist 2)) (list-ref templist (- dimlist 1))))) | |
(+ position 1) | |
(or (swap-check (list-ref templist (- dimlist 2)) (list-ref templist (- dimlist 1))) error?))) | |
(else | |
(display+ "Cond 3") | |
(slis-help | |
(append | |
(cut-list templist 1 (- position 1)) | |
(list | |
(get-first (list-ref templist (- position 1)) (list-ref templist position)) | |
(get-second (list-ref templist (- position 1)) (list-ref templist position))) | |
(cut-list templist (+ position 2) dimlist)) | |
(+ position 1) | |
(or (swap-check (list-ref templist (- position 1)) (list-ref templist position )) error?)))))))) | |
(slis-help origlist 1 #f)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment