Created
June 13, 2012 15:15
-
-
Save whostolebenfrog/2924693 to your computer and use it in GitHub Desktop.
Quicksort in clojure
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
;; Idea based on concatenating those values before the pivot with the pivot and those that come after the pivot. | |
;; | |
;; Call recursively until the length of the collection is 1 (then return just that item in the collection). | |
;; | |
;; Getting the items before and after the pivot is done by filtering the collection with an anonymous function | |
;; that takes a single argument and compares that to the pivot. | |
(defn quicksort | |
"Recursive quick sort implementation" | |
[items] | |
(if | |
(<= (count items) 1) items ;; for zero or 1 length collections just return | |
(let [pivot (first items) | |
others (rest items)] ;; don't really need these but aids readability | |
(concat | |
(quicksort (filter #(>= pivot %) others)) | |
[pivot] | |
(quicksort (filter #(< pivot %) others)))))) | |
(quicksort [2 3 1 4 5 8 2 1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how do we acess/call any value if we def it as nother variable?