Skip to content

Instantly share code, notes, and snippets.

@niftymonkey
Created May 5, 2012 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niftymonkey/2605926 to your computer and use it in GitHub Desktop.
Save niftymonkey/2605926 to your computer and use it in GitHub Desktop.
Insertion Sort
(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