Skip to content

Instantly share code, notes, and snippets.

@davidblurton
Created November 1, 2013 03:16
Show Gist options
  • Save davidblurton/7260533 to your computer and use it in GitHub Desktop.
Save davidblurton/7260533 to your computer and use it in GitHub Desktop.
Tried to write merge sort in Clojure. Nearly killed me.
(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