Paste: bubble-sort
Author: | jon |
Mode: | factor |
Date: | Wed, 11 Aug 2010 14:10:53 |
Plain Text |
USING: kernel locals math sequences ;
IN: bubble-sort
:: swap ( i j seq -- )
i seq nth
j seq nth i seq set-nth
j seq set-nth ;
: ?swap ( i j seq -- swapped? )
3dup [ nth ] curry bi@ > [ swap t ] [ 3drop f ] if ;
: ?swap-with-next ( i seq -- swapped? )
[ dup 1 + ] [ ?swap ] bi* ;
: (sort) ( seq -- swapped? )
[ length 1 - iota ] keep
[ ?swap-with-next ] curry [ or ] map-reduce ;
: bubble-sort ( seq -- sorted-seq )
dup [ (sort) ] curry loop ;
New Annotation