Skip to content

Instantly share code, notes, and snippets.

@whostolebenfrog
Created June 13, 2012 15:15
Show Gist options
  • Save whostolebenfrog/2924693 to your computer and use it in GitHub Desktop.
Save whostolebenfrog/2924693 to your computer and use it in GitHub Desktop.
Quicksort in clojure
;; 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])
@Huarastaca
Copy link

how do we acess/call any value if we def it as nother variable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment