Skip to content

Instantly share code, notes, and snippets.

@andskiba
Created December 10, 2014 10:27
Show Gist options
  • Save andskiba/88bdfeeade604601f7af to your computer and use it in GitHub Desktop.
Save andskiba/88bdfeeade604601f7af to your computer and use it in GitHub Desktop.
Clojure merge sort
(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