Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Created October 21, 2011 11:20
Show Gist options
  • Save ympbyc/1303587 to your computer and use it in GitHub Desktop.
Save ympbyc/1303587 to your computer and use it in GitHub Desktop.
gnome sort in jslisp
(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)
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);
[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