home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / sampler / 02 / sort / sort3.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-02-29  |  2.0 KB  |  61 lines

  1. procedure Sort(Size,Count : Word; var List);
  2. {
  3.    preconditions:  Size has a value of 1, 2, 4, or 6
  4.                    Count is in the range 0..Limit
  5.                    List is an array of type ShortInt, Integer,
  6.                      LongInt, or Real
  7.                    the upper bound of Limit and the type of List
  8.                      are determined by Size as follows:
  9.                      Size  max value of Limit   type of List
  10.                      ----  ------------------   ------------
  11.                        1        64,000            ShortInt
  12.                        2        32,000            Integer
  13.                        4        16,000            LongInt
  14.                        6        10,667             Real
  15.     postconditions: The elements 1..Count of List are sorted
  16.                      in ascending order
  17. }
  18. var
  19.   SList        : array[1..64000] of ShortInt absolute List;
  20.   IList        : array[1..32000] of Integer  absolute List;
  21.   LList        : array[1..16000] of LongInt  absolute List;
  22.   RList        : array[1..10667] of Real     absolute List;
  23.   Top,Min,K    : Word;
  24.  
  25.   function LessThan(I,J : Word) : Boolean;
  26.   begin
  27.     case Size of
  28.       1 : LessThan := SList[I] < SList[J];
  29.       2 : LessThan := IList[I] < IList[J];
  30.       4 : LessThan := LList[I] < LList[J];
  31.       6 : LessThan := RList[I] < RList[J];
  32.       else LessThan := False
  33.     end
  34.   end; { of locproc LessThan }
  35.  
  36.   procedure Swap(I,J : Word);
  37.   var
  38.     K          : Word;
  39.     Temp       : ShortInt;
  40.     ch         : char;
  41.   begin
  42.     I := 1 + (I-1)*Size;
  43.     J := 1 + (J-1)*Size;
  44.     for K := 0 to Size-1 do begin
  45.       Temp := SList[I+K];
  46.       SList[I+K] := SList[J+K];
  47.       SList[J+K] := Temp
  48.     end
  49.   end; { of locproc Swap }
  50.  
  51. begin
  52.   for Top := 1 to Count-1 do begin
  53.     Min := Top;
  54.     for K := Top+1 to Count do
  55.       if LessThan(K,Min)
  56.         then Min := K;
  57.     if (Min <> Top)
  58.       then Swap(Min,Top);
  59.   end;
  60. end; { of proc Sort }
  61.