factor/basis/sorting/insertion/insertion.factor

17 lines
426 B
Factor
Raw Normal View History

USING: locals sequences kernel math ;
IN: sorting.insertion
<PRIVATE
2008-07-18 20:22:59 -04:00
:: insert ( seq quot: ( elt -- elt' ) n -- )
n zero? [
n n 1- [ seq nth quot call ] bi@ >= [
n n 1- seq exchange
seq quot n 1- insert
] unless
2008-07-18 20:22:59 -04:00
] unless ; inline recursive
PRIVATE>
: insertion-sort ( seq quot -- )
! quot is a transformation on elements
over length [ insert ] with with each ; inline