]> gitweb.factorcode.org Git - factor.git/blob - basis/sorting/insertion/insertion.factor
2a2ac4134a1e527d4892a26c0e22d790bdbf0266
[factor.git] / basis / sorting / insertion / insertion.factor
1 USING: kernel locals math sequences sequences.private ;
2 IN: sorting.insertion
3
4 <PRIVATE
5
6 :: insert ( ... seq quot: ( ... elt -- ... elt' ) n -- ... )
7     n zero? [
8         n n 1 - [ seq nth-unsafe ] bi@
9         2dup [ quot call ] bi@ >= [ 2drop ] [
10             n 1 - n [ seq set-nth-unsafe ] bi-curry@ bi*
11             seq quot n 1 - insert
12         ] if
13     ] unless ; inline recursive
14
15 PRIVATE>
16
17 : insertion-sort ( ... seq quot: ( ... elt -- ... elt' ) -- ... )
18     ! quot is a transformation on elements
19     over length [ insert ] 2with 1 -rot (each-integer) ; inline