\ Selection Sort
\ Taken from http://rosettacode.org 
\ 4tH version 2011, Hans Bezemer

[UNDEFINED] sort     [IF]
[UNDEFINED] precedes [IF]
defer precedes                         ( addr addr -- flag )
[THEN]                                 \ compatible with QSORT

[DEFINED] 4TH# [IF]                    ( start end -- least )
: (selection) over cell+ do i @ over @ precedes if drop i then loop ;
[ELSE]                                 ( start end -- least )
: (selection) over cell+ do i @ over @ precedes if drop i then 1 cells +loop ;
[THEN]

: sort                                 ( array len -- )
  bounds over                          ( end start end )
  cell- swap do                        ( end )
    i over (selection)                 ( end least )
    i @ over @ i ! swap !
[DEFINED] 4TH# [IF]
  loop
[ELSE]
  1 cells +loop
[THEN]
  drop ;

[DEFINED] 4TH# [IF] hide (selection) [THEN]
[THEN]

