Created
December 10, 2014 10:27
-
-
Save andskiba/88bdfeeade604601f7af to your computer and use it in GitHub Desktop.
Clojure merge sort
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
(ns misc.merges | |
(:require [joy.qs :refer [rand-ints]])) | |
(defn merge-sort [xs] | |
(if (<= (count xs) 1) | |
xs | |
(let [[left right] (split-at (/ (count xs) 2) xs)] | |
(mrge (merge-sort left) (merge-sort right))))) | |
(defn mrge [xs ys] | |
(loop [xs xs, ys ys, acc []] | |
(if (or (empty? xs) (empty? ys)) | |
(concat acc xs ys) | |
(let [x (first xs) | |
y (first ys)] | |
(if (< x y) | |
(recur (next xs) ys (vec (conj acc x))) | |
(recur xs (next ys) (vec (conj acc y)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment