home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / tipi / sort.tpi < prev    next >
Text File  |  1993-11-07  |  680b  |  35 lines

  1. # SORT.TPI
  2. # by Kent Peterson
  3. # Recursive sort of values on the stack
  4. # This routine will sort the stack, placing the smallest
  5. # items on the top and the largest items on the bottom.
  6. # Algorithm by Dr. Richard H. Turpin, Purdue University
  7. # from an article in Forth Dimensions Volume V, No. 2, p 16
  8.  
  9. defvar flag
  10.  
  11. define sink
  12.  depth 1 > if over over
  13.              < if swap 0 flag store endif
  14.              push sink pop
  15.            endif
  16. enddef
  17.  
  18. define sort
  19.  depth
  20.  do 1 flag store
  21.     sink
  22.     flag fetch if leave endif
  23.  loop
  24. enddef
  25.  
  26.  
  27. # now test the sort
  28. 10 7 15 11 0 -1 24 sort
  29.  
  30. # and print it out
  31. depth do print " " print$ loop
  32.  
  33. begin key until
  34.  
  35.