1 ! Copyright (C) 2014 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: combinators kernel locals math math.order sequences
11 :: (quicksort) ( seq from to -- )
13 from to + 2/ seq nth-unsafe :> pivot
16 [ over seq nth-unsafe pivot before? ] [ [ 1 + ] dip ] while
17 [ dup seq nth-unsafe pivot after? ] [ 1 - ] while
19 [ seq exchange-unsafe ]
20 [ [ 1 + ] [ 1 - ] bi* ] 2bi
24 [ seq from ] dip (quicksort)
25 [ seq ] dip to (quicksort)
27 ] when ; inline recursive
31 : quicksort ( seq -- )
32 0 over length 1 - (quicksort) ;