Sorting algorithms/Gnome sort: Difference between revisions
Content added Content deleted
(→{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details) |
(Add solution in Factor) |
||
Line 811: | Line 811: | ||
if i=1 then loop j (j+1) else loop (i-1) j |
if i=1 then loop j (j+1) else loop (i-1) j |
||
loop 1 2</lang> |
loop 1 2</lang> |
||
=={{header|Factor}}== |
|||
USING: kernel math sequences ; |
|||
IN: rosetta-code.gnome-sort |
|||
: inc-pos ( pos seq -- pos' seq ) |
|||
[ 1 + ] dip ; inline |
|||
: dec-pos ( pos seq -- pos' seq ) |
|||
[ 1 - ] dip ; inline |
|||
: take-two ( pos seq -- elt-at-pos-1 elt-at-pos ) |
|||
[ dec-pos nth ] [ nth ] 2bi ; inline |
|||
: need-swap? ( pos seq -- pos seq ? ) |
|||
over 1 < [ f ] [ 2dup take-two > ] if ; |
|||
: swap-back ( pos seq -- pos seq' ) |
|||
[ take-two ] 2keep |
|||
[ dec-pos set-nth ] 2keep |
|||
[ set-nth ] 2keep ; |
|||
: gnome-sort ( seq -- sorted-seq ) |
|||
1 swap [ 2dup length < ] [ |
|||
2dup [ need-swap? ] [ swap-back dec-pos ] while |
|||
2drop inc-pos |
|||
] while nip ; |
|||
Example: |
|||
IN: scratchpad '''USE: rosetta-code.gnome-sort''' |
|||
Loading resource:extra/rosetta-code/gnome-sort/gnome-sort.factor |
|||
IN: scratchpad '''V{ 10 9 5 7 4 3 6 8 1 2 } gnome-sort .''' |
|||
V{ 1 2 3 4 5 6 7 8 9 10 } |
|||
=={{header|Fantom}}== |
=={{header|Fantom}}== |