Last active
August 29, 2015 14:11
-
-
Save keyvanakbary/30e29aeee16bcafd8f92 to your computer and use it in GitHub Desktop.
Sorting algorithm O(N^2). More info here http://en.wikipedia.org/wiki/Insertion_sort and here https://www.youtube.com/watch?v=8oJS1BMKE64
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 insertionsort [lns] | |
| (let | |
| [insert | |
| (fn insert [n lns] | |
| (if (or (empty? lns) (< n (first lns))) | |
| (cons n lns) | |
| (cons (first lns) | |
| (insert n (rest lns)))))] | |
| (loop [sns '() | |
| lns lns] | |
| (if (empty? lns) | |
| sns | |
| (recur | |
| (insert (first lns) sns) | |
| (rest lns)))))) | |
| (insertionsort '(3 4 12 3 45 1 -1 -30)) | |
| ;;=> (-30 -1 1 3 3 4 12 45) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment