Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
Last active May 7, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zehnpaard/500cb06b6fb146ec87290dadbd57b103 to your computer and use it in GitHub Desktop.
Save zehnpaard/500cb06b6fb146ec87290dadbd57b103 to your computer and use it in GitHub Desktop.
Clojure Quick Sort with random pivot
(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