Last active
June 16, 2022 01:02
-
-
Save abcdw/81138aa8ee187dbbd8b0 to your computer and use it in GitHub Desktop.
merge sort in clojure
This file contains hidden or 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 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