Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: SORTS and "I" - pt2 Message-ID: <151.UUL1.3#5129@willett.UUCP> Date: 5 Jan 90 02:55:44 GMT Organization: Latest Link in ForthNet Chain Lines: 39 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 \ This & could : smallest ( -- n ) @ ; \ be VARs, too. : smallest! ( n -- ) ! ; VARIABLE \ COUNTER contains the number of items to be sorted : COUNTER ( -- n ) @ ; : {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'