Created
October 21, 2011 11:20
-
-
Save ympbyc/1303587 to your computer and use it in GitHub Desktop.
gnome sort in jslisp
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
(define swap (lambda (a b lis) | |
((. (vector) concat) | |
((. lis slice) 0 a) | |
(get lis b) | |
(get lis a) | |
((. lis slice) (+ b 1))))) | |
(define gnomesort (lambda (lis i) | |
(if (>= i (. lis length)) | |
lis | |
(if (< i 1) | |
(gnomesort lis 1) | |
(if (<= (get lis (- i 1)) (get lis i)) | |
(gnomesort lis (+ i 1)) | |
(gnomesort (swap (- i 1) i lis) | |
(- i 1))))))) | |
(gnomesort (vector 4 5 4 5 0 7 2 1) 1) |
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
var swap = function (a,b,lis){ | |
return (((vector)() . concat))(((lis . slice))(0,a),(get)(lis,b),(get)(lis,a),((lis . slice))((b + 1)))}; | |
var gnomesort = function (lis,i){ | |
return (i >= (lis . length)) ? lis : (i < 1) ? (gnomesort)(lis,1) : ((get)(lis,(i - 1)) <= (get)(lis,i)) ? (gnomesort)(lis,(i + 1)) : (gnomesort)((swap)((i - 1),i,lis),(i - 1))}; | |
(gnomesort)((vector)(4,5,4,5,0,7,2,1),1); |
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
[0,1,2,4,4,5,5,7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment