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

  1. {   Arrrggghh. I hate Bubble sorts. Why don't you use Merge sort? It's a hell
  2.  of a lot faster and if you have a large enough stack, there wouldn't be
  3.  any problems. if you were not interested in doing a recursive sort, then
  4.  here is an example fo the Shell sort which is one of the most efficient
  5.  non recursive sorts around.
  6. }
  7.  
  8.  
  9. Const
  10.     Max = 50;
  11. Type
  12.     ArrayType = Array[1..Max] of Integer;
  13.  
  14. Var
  15.     Data, Temp    : ArrayType;
  16.     Response      : Char;
  17.     X, Iteration  : Integer;
  18.  
  19. Procedure ShellSort (Var Data : ArrayType;Var Iteration : Integer;
  20.                                             NumberItems : Integer);
  21.  
  22. Procedure Sort (Var Data : ArrayType; Var Iteration : Integer;
  23.                              NumberItems, Distance : Integer);
  24.  
  25. Var
  26.    X, Y : Integer;
  27.  
  28. begin   {Sort}
  29.    Iteration := 0;
  30.    For Y := Distance + 1 to NumberItems Do
  31.       begin  {For}
  32.          X := Y - Distance;
  33.          While X > 0 Do
  34.             begin   {While}
  35.                if Data[X+Distance] < Data[X] then
  36.                   begin   {if}
  37.                      Switch (Data[X+Distance], Data[X], Iteration);
  38.                      X := X - Distance;
  39.                      Iteration := Iteration + 1
  40.                   end     {if}
  41.                else
  42.                   X := 0;
  43.             end;    {While}
  44.       end    {For}
  45. end;    {Sort}
  46.  
  47. begin   {ShellSort}
  48.    Distance := NumberItems div 2;
  49.    While Distance > 0 do
  50.       begin   {While}
  51.          Sort (Data, Iteration, NumberItems, Distance);
  52.          Distance := Distance div 2
  53.       end;    {While}
  54. end;    {ShellSort}
  55.