Skip to content

Instantly share code, notes, and snippets.

Created November 26, 2015 11:45
What would you like to do?
Clojure lazy QS
(defn sort-parts
"Lazy, tail-recursive, incremental quicksort. Works against
and creates partitions based on the pivot, defined as 'work'."
(loop [[part & parts] work]
(if-let [[pivot & xs] (seq part)]
(let [smaller? #(< % pivot)]
(recur (list*
(filter smaller? xs)
(remove smaller? xs)
(when-let [[x & parts] parts]
(cons x (sort-parts parts)))))))
(defn qsort [xs]
(sort-parts (list xs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment