Created
May 5, 2012 22:13
-
-
Save niftymonkey/2605926 to your computer and use it in GitHub Desktop.
Insertion Sort
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
(defn insert [val lst] | |
(cond | |
(empty? lst) (list val) ; if it's an empty list, initialize it | |
(> (first lst) val) (conj lst val) ; if the first item is smaller than the first of lst, just conj | |
:else | |
(conj (insert val (rest lst)) (first lst)))) ; otherwise we need to keep calling insert recursively | |
(defn insertion-sort [lst] | |
(loop [list lst result '()] ; create local variables for our result set and the list passed in | |
(if (empty? list) result ; if the list passed in was empty, just return our empty result list | |
(recur (rest list) (insert (first list) result))))) ; otherwise, recursively insert into the result list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment