Last active
May 7, 2016 11:27
-
-
Save zehnpaard/500cb06b6fb146ec87290dadbd57b103 to your computer and use it in GitHub Desktop.
Clojure Quick Sort with random pivot
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
(defn quick-sort | |
"Sort sequence using simple recursion based on splitting by randomly chosen pivot" | |
[xs] | |
(if (empty? (rest xs)) | |
xs | |
(let [pivot-index | |
(rand-int (count xs)) | |
[heads [pivot & tails]] | |
(split-at pivot-index xs) | |
xs-ex-pivot | |
(concat heads tails) | |
{lesser true, bigger false} | |
(group-by #(< % pivot) xs-ex-pivot)] | |
(concat (quick-sort lesser) | |
[pivot] | |
(quick-sort bigger))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment