[comp.lang.forth] SORTS and "I" - pt2

ForthNet@willett.UUCP (ForthNet articles from GEnie) (01/05/90)

 Date: 12-23-89 (17:18)              Number: 2705 (Echo)
   To: ALL                           Refer#: NONE
 From: STEVE PALINCSAR                 Read: (N/A)
 Subj: SORTS and "I" - pt2           Status: PUBLIC MESSAGE

 \ The SELECTION SORT (Algorithm S from Knuth Vol. 3)
 .
 \ ELEMENT is a string array.  n ELEMENT should leave the 
 \ address of the nth string in the array on the stack
 VARIABLE <smallest>          \ This & <COUNTER> could
 : smallest   ( -- n ) <smallest> @ ;    \ be VARs, too.  
 : smallest!  ( n -- ) <smallest> ! ;    
 VARIABLE <COUNTER>
 \ COUNTER contains the number of items to be sorted
 : COUNTER  ( -- n ) <COUNTER> @ ;
 : {exchange}  ( $addr1 $addr2 -- )
 \ swap the counted strings at addr1 & addr2
   ( whatever code is neccessary to exchange counted strings )
   ( so whatever was at addr1 will be at addr2 and vice versa ) ;
 : larger?  ( element1  element2 -- f )
 \ True if element1 should sort after element2
    ( whatever code necessary to compare two strings, leaving a )
    ( true flaag if first ELEMENT should be later in sequence )
    ( than the second string being compared  )  ;
 : SELECTION_SORT
    COUNTER  0 DO
       I smallest!
       COUNTER  I  DO
           smallest ELEMENT  I ELEMENT  larger?  
           IF  I smallest!  THEN
       LOOP   
       smallest ELEMENT  I ELEMENT  {exchange}
    LOOP  ;
 .
 [cont'd]
-----
This message came from GEnie via willett through a semi-automated program.
Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'