home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / ifp / part01 / fproot / demo / InsertSort < prev    next >
Encoding:
Text File  |  1987-07-05  |  595 b   |  26 lines

  1. (*
  2.  * InsertSort
  3.  *
  4.  * This function sorts a sequence of numbers or strings into ascending order
  5.  * using insertion sort.
  6.  *
  7.  * Examples:
  8.  *
  9.  *      <3 1 4 1 5 9 2> : InsertSort == <1 1 2 3 4 5 9>
  10.  *
  11.  *      <all work and no play> : InsertSort == <all and no play work>
  12.  *
  13.  * The sequence may not mix strings and numbers.
  14.  *)
  15. DEF InsertSort AS
  16.    IF null THEN id            (* Check for trivial case *)
  17.    ELSE
  18.       [tl,[1]] | apndr | 
  19.       INSERT
  20.      {[Element,Seq] := id}
  21.          {[Left,Right] := [Seq, distl | FILTER > END | length] | [takel,dropl]}
  22.      [Left,[Element],Right] | cat
  23.       END
  24.    END;
  25.  
  26.