Skip to content

Instantly share code, notes, and snippets.

@vu3rdd
Created January 20, 2010 19:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vu3rdd/282146 to your computer and use it in GitHub Desktop.
(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