Created
April 9, 2012 03:52
-
-
Save git2samus/2341265 to your computer and use it in GitHub Desktop.
merge-sort as a recursive function
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 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