Created
March 20, 2012 13:08
-
-
Save alco/2135276 to your computer and use it in GitHub Desktop.
Mergesort 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
(defn merge-seqs | |
"Merges two sorted sequences into a single sorted sequence" | |
([left right] | |
(merge-seqs (list left right))) | |
([[left right]] | |
(loop [l left, r right, result []] | |
(let [lhead (first l), rhead (first r)] | |
(cond | |
(nil? lhead) (concat result r) | |
(nil? rhead) (concat result l) | |
(<= lhead rhead) (recur (rest l) r (conj result lhead)) | |
true (recur l (rest r) (conj result rhead))))))) | |
(defn mergesort | |
"Produces a sorted sequence from an input sequence. | |
Works best with vectors (since it uses 'count' internally)." | |
[xs] | |
((fn mergesort-counted [xs n] | |
(if (<= n 1) | |
xs | |
(let [middle (bit-shift-right n 1)] ; fast division by 2 | |
(merge-seqs (map mergesort-counted | |
(split-at middle xs) ; two halves | |
[middle (- n middle)]))))) ; count of each half | |
xs (count xs))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a lazy version that works on multiple sequences: http://blog.malcolmsparks.com/?p=42. Pretty nice.