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
(ns algo.sorting.insert) | |
;; non-lazy version | |
(defn insert | |
"insert key into the lst which is assumed to be already sorted" | |
[key lst] | |
(if (empty? lst) | |
(seq [key]) | |
(let [[x & xs] lst] | |
(if (< key x) | |
(cons key lst) | |
(cons x (insert key xs)))))) | |
(defn in-sort | |
"insertion sort of the collection" | |
[coll] | |
(if (empty? coll) | |
nil | |
(insert (first coll) (in-sort (rest coll))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment