Skip to content

Instantly share code, notes, and snippets.

@git2samus
Created April 9, 2012 03:52
Show Gist options
  • Select an option

  • Save git2samus/2341265 to your computer and use it in GitHub Desktop.

Select an option

Save git2samus/2341265 to your computer and use it in GitHub Desktop.
merge-sort as a recursive function
(defn merge-step
([seq1 seq2]
(merge-step [] seq1 seq2))
([result seq1 seq2]
(let [head1 (first seq1)
head2 (first seq2)]
(cond
(nil? head1) (concat result seq2)
(nil? head2) (concat result seq1)
:else (if (< head1 head2)
(recur (conj result head1) (rest seq1) seq2)
(recur (conj result head2) seq1 (rest seq2)))))))
(defn divide-step [coll]
(let [coll-length (count coll)]
(if (> coll-length 1)
(let [[seq1 seq2] (split-at (quot coll-length 2) coll)]
(merge-step (divide-step seq1) (divide-step seq2)))
coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment