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

  1. {
  2. >Why can't Borland come out With a Universal sort since they made the
  3. >Program.. <G>
  4.  
  5. I guess there's no such thing as a "universal" sort... There are a few very
  6. good sorting algorithms, and depending on some factors, you just have to
  7. choose the one that best fits your needs!
  8.  
  9. Here's an update to my ELEVAtoR sort, this one's even faster!
  10. }
  11.  
  12. Program mysort;
  13.  
  14. Uses Crt;
  15.  
  16. Const
  17.   max = 1000;
  18.  
  19. Type
  20.   list = Array[1..max] of Word;
  21.  
  22. Var
  23.   data  : list;
  24.   dummy : Word;
  25.  
  26.  
  27. Procedure elevatorsort(Var a: list; hi: Word);
  28.  
  29. Var
  30.   dummy,
  31.   low,
  32.   peak,
  33.   temp,
  34.   temp2  : Word;
  35.  
  36. begin
  37.   peak   := 1;
  38.   low    := 1;
  39.   temp2  := a[low + 1];
  40.   Repeat
  41.     temp  := a[low];
  42.     if temp > temp2 then
  43.     begin
  44.       a[low]     := temp2;
  45.       a[low + 1] := temp;
  46.       if low <> 1 then dec(low);
  47.     end
  48.       else
  49.     begin
  50.       inc(peak);
  51.       low:=peak;
  52.       if low <> hi then temp2:=a[low + 1];
  53.     end;
  54.   Until low = hi;
  55. end;
  56.  
  57. begin
  58.   ClrScr;
  59.   Writeln('Generating ', max ,' random numbers...');
  60.   randomize;
  61.   For dummy:=1 to max do data[dummy]:=random(65535);
  62.   Writeln('Sorting random numbers...');
  63.   elevatorsort(data,max);
  64.   For dummy:=1 to max do Write(data[dummy]:5,'   ');
  65. end.
  66.