Quicksort in Clojure Published Tue Feb 14 02:00:00 SAST 2017
(This was a Valentine's Day post.)
Who can understand the Order of Love? Let's find out with a Quicksort implementation in Clojure:
(defn quicksort [coll]
(if (seq coll)
(let [pivot (rand-nth coll)
pivots (filter #(= % pivot) coll)
less (filter #(< % pivot) coll)
greater (filter #(> % pivot) coll)]
(concat (quicksort less) pivots (quicksort greater)))
coll))
Example 1
(quicksort [49 11 37 2 18])
=> (2 11 18 37 49)
Example 2
(->> [4326 55357 10086 10087 56460 10084 9753 10163 63 10083 10085 9825 9829]
(quicksort)
(map char)
(apply str))