home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / sorting.swg / 0006_COMB1.PAS.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  1.2 KB  |  59 lines

  1. {
  2. >Has anyone successfully converted the Combsort algorithm (I think it was
  3. >published in DDJ or Byte about two years ago) from C to Pascal?  I've
  4. >lost the original C source For this, but if anyone has any info, I would
  5. >appreciate it.
  6. }
  7.  
  8. Program TestCombSort; { Byte magazine, April '91 page 315ff }
  9. Const
  10.   Size = 25;
  11. Type
  12.   SortType = Integer;
  13. Var
  14.   A: Array [1..size] of SortType;
  15.   i: Word;
  16.  
  17. Procedure CombSort (Var Ain);
  18. Var
  19.   A: Array [1..Size] of SortType Absolute Ain;
  20.   Switch: Boolean;
  21.   i, j, Gap: Word;
  22.   Hold: SortType;
  23. begin
  24.   Gap := Size;
  25.   Repeat
  26.     Gap := Trunc (Gap / 1.3);
  27.     if Gap < 1 then
  28.       Gap := 1;
  29.     Switch := False;
  30.     For i := 1 to Size - Gap do
  31.     begin
  32.       j := i + Gap;
  33.       if A [i] > A [j] then { swap }
  34.       begin
  35.         Hold := A [i];
  36.         A [i] := A [j];
  37.         A [j] := Hold;
  38.         Switch := True;;
  39.       end;
  40.     end;
  41.   Until (Gap = 1) and not Switch;
  42. end;
  43.  
  44. begin
  45.   Randomize;
  46.   For i := 1 to Size do
  47.     A [i] := Random (32767);
  48.   WriteLn;
  49.   WriteLn ('Unsorted:');
  50.   For i := 1 to Size do
  51.     Write (A [i]:8);
  52.   WriteLn;
  53.   CombSort (A);
  54.   WriteLn ('Sorted:');
  55.   For i := 1 to Size do
  56.     Write (A [i]:8);
  57.   WriteLn;
  58. end.
  59.