Skip to content

Instantly share code, notes, and snippets.

@abcdw
Last active June 16, 2022 01:02
Show Gist options
  • Save abcdw/81138aa8ee187dbbd8b0 to your computer and use it in GitHub Desktop.
Save abcdw/81138aa8ee187dbbd8b0 to your computer and use it in GitHub Desktop.
merge sort in clojure
(defn mrg
([L R] (mrg L R []))
([[l & left :as L] [r & right :as R] M]
(if (or (empty? L) (empty? R))
(concat M L R)
(if (<= l r)
(recur left R (conj M l))
(recur L right (conj M r))))))
(defn msort [arr]
(if (empty? (rest arr))
arr
(let [sides (split-at (/ (count arr) 2) arr)]
(mrg (msort (sides 0)) (msort (sides 1))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment