Created
November 1, 2013 03:16
-
-
Save davidblurton/7260533 to your computer and use it in GitHub Desktop.
Tried to write merge sort in Clojure. Nearly killed me.
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
(mergesort [6 4 2 3 5 1]) | |
(defn mergesort [x] | |
(if (< (count x) 2) x | |
(let [half (quot (count x) 2)] | |
(merge (mergesort (take half x)) (mergesort (drop half x)))))) | |
(defn merge[a, b] | |
(if (empty? a) b | |
(if (empty? b) a | |
(if (< (first a) (first b)) (cons (first a) (merge (rest a) b)) | |
(cons (first b) (merge a (rest b))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment