Last active
December 12, 2015 04:08
-
-
Save arrdem/4712024 to your computer and use it in GitHub Desktop.
A simple qsort implementation in Clojure
This file contains hidden or 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 %remove-first [val seq] | |
(loop [h seq | |
t []] | |
(if (empty? h) | |
t | |
(if (= (first h) val) | |
(concat t (rest h)) | |
(recur (rest h) (conj t (first h))))))) | |
(defn qsort [seq] | |
(if (empty? seq) [] | |
(let [pivot (rand-nth seq) | |
seq (%remove-first pivot seq) | |
left (filter #(> pivot %1) seq) | |
right (filter #(<= pivot %1) seq)] | |
(concat (qsort left) [pivot] (qsort right))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment