home *** CD-ROM | disk | FTP | other *** search
/ CD-X 1 / cdx_01.iso / demodisc / tyrant / rendezes / rendezes.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1994-01-05  |  10.5 KB  |  339 lines

  1. program Rendezes;
  2. { Rendezôalgoritmusok összehasonlító elemzése }
  3. uses
  4.   crt;
  5. const
  6.   MaxExample = 2500;
  7.   BarLength = 58;
  8.   MS = 0.055;
  9. type
  10.   TExamples = array [1..MaxExample] of word;
  11.   PNode = ^TNode;
  12.   TNode = record
  13.     Value : word;
  14.     Left, Right, Parent : PNode;
  15.     end;
  16. var
  17.   Examples, Original : TExamples;              { rendezendô példaadatok    }
  18.   i, ConvertI : integer;
  19.   TimeStamp : longint absolute $0040:$006C;    { 55 ms-os órajel, BIOS     }
  20.   Started : longint;
  21.   LongestTime, Duration, Ratio : real;
  22.   SaveColor : byte;
  23.   Tree : PNode;
  24.  
  25. { SEGÉDRUTINOK *********************************************************** }
  26. procedure Swap (i, j : word);                  { két elem kicserélése      }
  27. var
  28.   dummy : word;
  29. begin
  30.   dummy := Examples [i]; Examples [i] := Examples [j]; Examples [j] := dummy;
  31. end; { Swap }
  32.  
  33. function Bar (Duration : real) : string;       { idôjelzô csík             }
  34. var
  35.   i, len : byte;
  36.   dummy : string;
  37. begin
  38.   dummy := ''; len := round (Duration / Ratio);
  39.   for i := 1 to len  do dummy := dummy + #219;
  40.   for i := len + 1 to BarLength do dummy := dummy + '_';
  41.   Bar := dummy;
  42. end; { Bar }
  43.  
  44. procedure Results (Duration : real);
  45. begin
  46.   TextAttr := LightGreen + Blue shl 4; write (Bar (Duration));
  47.   TextAttr := LightRed + Blue shl 4; writeln (Duration:7:3, ' s');
  48. end; { Results }
  49.  
  50. { FA-RUTINOK ************************************************************* }
  51. procedure InOrder (Tree : PNode);
  52. begin
  53.   if Tree <> nil then
  54.     begin
  55.       InOrder (Tree^.Left);
  56.       Examples [ConvertI] := Tree^.Value; inc (ConvertI);
  57.       InOrder (Tree^.Right);
  58.     end;
  59. end; { InOrder }
  60.  
  61. { RENDEZºALGORITMUSOK **************************************************** }
  62. procedure BubbleSort;
  63. { Szokásos buborékos rendezés                                              }
  64. var
  65.   j : word;
  66. begin
  67.   for i := MaxExample downto 1 do
  68.     for j := 1 to i do
  69.       if Examples [j] > Examples [j + 1] then
  70.         Swap (j, j + 1);
  71. end; { BubbleSort }
  72.  
  73. procedure ImpBubbleSort;
  74. { Javított buborékos rendezés - csak addig cserélünk, amíg van mit         }
  75. var
  76.   Swapped : Boolean;
  77.   start : word;
  78. begin
  79.   start := 1;
  80.   repeat
  81.     Swapped := false;
  82.     for i := start to pred (MaxExample) do
  83.       if Examples [i] > Examples [i + 1] then
  84.         begin Swap (i, i + 1); Swapped := true; end;
  85.     inc (start);
  86.   until not Swapped;
  87. end; { ImpBubbleSort }
  88.  
  89. procedure ShellSort;
  90. { B W Kernighan - P J Plauger: A programozás magasiskolája. Mûszaki 1982   }
  91. { GAP távolságú elemekbôl álló részsorozatokat rendezünk                   }
  92. var
  93.   gap, j, jg : integer;
  94. begin
  95.   gap := MaxExample div 2;
  96.   while gap > 0 do
  97.     begin
  98.       for i := succ (gap) to MaxExample do
  99.         begin
  100.           j := i - gap;
  101.           while j > 0 do
  102.             begin
  103.               jg := j + gap;
  104.               if Examples [j] <= Examples [jg] then j := 0
  105.               else Swap (j, jg);
  106.               dec (j, gap);
  107.             end;
  108.         end;
  109.       gap := gap div 2;
  110.     end;
  111. end; { ShellSort }
  112.  
  113. procedure JumpUp;
  114. { L Artiaga - L D Davis: Algoritmusok és FORTRAN programjaik. Mûszaki 1977 }
  115. { Hasonló a buborékoshoz, de csak az elôl rendezôdô elemek utáni, egyre
  116.   csökkenô hosszúságú még rendezetlen részen dolgozunk; nem a szomszédosokat
  117.   cseréljük, hanem a rendezetlen balszélét a jobbról jövô elemekkel        }
  118. var
  119.   i, j : word;
  120. begin
  121.   for i := 1 to pred (MaxExample) do
  122.     for j := i + 1 to MaxExample do
  123.       if Examples [i] > Examples [j] then Swap (i, j);
  124. end; { JumpUp }
  125.  
  126. procedure InsertSort;
  127. { Szokásos beszúrásos rendezés                                             }
  128. var
  129.   i, j : word;
  130. begin
  131.   for i := 2 to MaxExample do
  132.     begin
  133.       j := i;
  134.       while (j > 1) and (Examples [j - 1] > Examples [j]) do
  135.         begin Swap (j, j - 1); dec (j); end;
  136.     end;
  137. end; { InsertSort }
  138.  
  139. procedure InsertSortB;
  140. { Jon Louis Bentley: Programming Pearls                                    }
  141. { Szokásos beszúrásos rendezés, Jon L Bentley optimalizálásával            }
  142. var
  143.   i, j, temp : word;
  144. begin
  145.   for i := 2 to MaxExample do
  146.     begin
  147.       j := i; temp := Examples [j];
  148.       while (j > 1) and (Examples [j - 1] > temp) do
  149.         begin Examples [j] := Examples [j - 1]; dec (j); end;
  150.       Examples [j] := temp;
  151.     end;
  152. end; { InsertSortB }
  153.  
  154. procedure SortI;
  155. { L Artiaga - L D Davis: Algoritmusok és FORTRAN programjaik. Mûszaki 1977 }
  156. { Sorban mindenkit kicserélünk a minimumelemmel                            }
  157. var
  158.   i : word;
  159.  
  160.   function Minimum : word;
  161.   var
  162.     j, Min, Temp : word;
  163.   begin
  164.     Min := Examples [i];
  165.     for j := 1 to MaxExample do
  166.       if Min < Examples [j] then
  167.         begin Temp := Min; Min := Examples [j]; Examples [j] := Temp; end;
  168.     Minimum := Min;
  169.   end; { Minimum of SortI }
  170.  
  171. begin
  172.   for i := 1 to MaxExample do Examples [i] := Minimum;
  173. end; { SortI }
  174.  
  175. procedure HeapSort;
  176. { Szokásos heapsort                                                        }
  177.  
  178.   procedure Stack (i, j : word);
  179.   var
  180.     k : word;
  181.   begin
  182.     k := 2 * i;
  183.     if k <= j then
  184.       begin
  185.         if k < j then
  186.           if Examples [k] < Examples [k + 1] then inc (k);
  187.         if Examples [i] < Examples [k] then
  188.           begin Swap (i, k); Stack (k, j); end;
  189.       end;
  190.   end; { Stack of HeapSort }
  191.  
  192. begin
  193.   for i := (MaxExample div 2) downto 1 do Stack (i, maxExample);
  194.   for i := MaxExample downto 2 do begin Swap (1, i); Stack (1, i-1); end;
  195. end; { HeapSort }
  196.  
  197. procedure QuickSort (left, right : word);
  198. { Szokásos quicksort rendezés                                              }
  199. var
  200.   up, down, compare : word;
  201. begin
  202.   up := left; down := right; compare := Examples [(left + right) div 2];
  203.   repeat
  204.     while Examples [up] < compare do inc (up);
  205.     while compare < Examples [down] do dec (down);
  206.     if up <= down then
  207.       begin Swap (up, down); inc (up); dec (down); end;
  208.   until up > down;
  209.   if left < down then QuickSort (left, down);
  210.   if up < right then QuickSort (up, right);
  211. end; { QuickSort }
  212.  
  213. procedure TreeSort;
  214. { Szokásos rendezés rendezôfával                                           }
  215. var
  216.   i : word;
  217.   P, Node : PNode;
  218.   HeapState : pointer;
  219. begin
  220.   Mark (HeapState); new (Tree);
  221.   Tree^.Value := Examples [1]; Tree^.Left := nil; Tree^.Right := nil;
  222.   for i := 2 to MaxExample do
  223.     begin
  224.       P := Tree;
  225.       while ((P^.Left <> nil) and (Examples [i] <= P^.Value)) or
  226.         ((P^.Right <> nil) and (Examples [i] > P^.Value)) do
  227.         if Examples [i] <= P^.Value then P := P^.Left else P := P^.Right;
  228.       new (Node); Node^.Parent := P; Node^.Value := Examples [i];
  229.       Node^.Left := nil; Node^.Right := nil;
  230.       if Examples [i] <= P^.Value then P^.Left := Node else P^.Right := Node;
  231.     end;
  232.   ConvertI := 1; InOrder (Tree);
  233.   Release (HeapState);
  234. end; { TreeSort }
  235.  
  236. procedure BinExch (left, right, digits : word);
  237. { Bináris csere rendezés                                                   }
  238. var
  239.   Mask, LeftMost, RightMost : word;
  240. begin
  241.   RightMost := right; LeftMost := left; Mask := $8000 shr (digits-1);
  242.   while (right > left) and (digits <= 16) do
  243.     begin
  244.       while (Examples [left] and Mask = 0) and (right >= left) do inc (left);
  245.       while (Examples [right] and Mask > 0) and (right >= left) do dec (right);
  246.       if right > left then Swap (right, left)
  247.       else
  248.         begin
  249.           BinExch (LeftMost, right, digits+1);
  250.           BinExch (left, RightMost, digits+1);
  251.         end;
  252.     end;
  253. end; { BinExch }
  254.  
  255. { FºPROGRAM ************************************************************** }
  256. begin
  257.   { Képernyôfeliratok                                                      }
  258.   SaveColor := TextAttr;
  259.   TextAttr := Yellow + Blue shl 4; clrscr;
  260.   TextAttr := White + Red shl 4;
  261.   writeln (' Rendezôalgoritmusok összehasonlító elemzése                                    ');
  262.   TextAttr := White + Blue shl 4;
  263.   writeln (' ', MaxExample, ' db természetes szám sorbarendezésének idôszükséglete a baloldali');
  264.   writeln (' oszlopban megadott algoritmusok felhasználásával'); writeln;
  265.   { Véletlen példaadatok generálása                                        }
  266.   Randomize;
  267.   for i := 1 to MaxExample do
  268.     Original [i] := Random (30000);
  269.   { Különbözô rendezôalgoritmusok hívása                                   }
  270.   TextAttr := Yellow + Blue shl 4; write (' Buborék   ');
  271.   Move (Original, Examples, sizeof (Original));
  272.   Started := TimeStamp;
  273.   BubbleSort;
  274.   Duration := (TimeStamp - Started) * MS;
  275.   LongestTime := Duration;                     { a leglassabb rendezéshez  }
  276.   Ratio := (LongestTime * 1.2) / BarLength;    { mérjük a többieket        }
  277.   Results (Duration);
  278.  
  279.   TextAttr := Yellow + Blue shl 4; write (' JumpUp    ');
  280.   Move (Original, Examples, sizeof (Original));
  281.   Started := TimeStamp;
  282.   JumpUp;
  283.   Results ((TimeStamp - Started) * MS);
  284.  
  285.   TextAttr := Yellow + Blue shl 4; write (' SORT I    ');
  286.   Move (Original, Examples, sizeof (Original));
  287.   Started := TimeStamp;
  288.   SortI;
  289.   Results ((TimeStamp - Started) * MS);
  290.  
  291.   TextAttr := Yellow + Blue shl 4; write (' Beszúr 1  ');
  292.   Move (Original, Examples, sizeof (Original));
  293.   Started := TimeStamp;
  294.   InsertSort;
  295.   Results ((TimeStamp - Started) * MS);
  296.  
  297.   TextAttr := Yellow + Blue shl 4; write (' BuborJav  ');
  298.   Move (Original, Examples, sizeof (Original));
  299.   Started := TimeStamp;
  300.   ImpBubbleSort;
  301.   Results ((TimeStamp - Started) * MS);
  302.  
  303.   TextAttr := Yellow + Blue shl 4; write (' Beszúr 2  ');
  304.   Move (Original, Examples, sizeof (Original));
  305.   Started := TimeStamp;
  306.   InsertSortB;
  307.   Results ((TimeStamp - Started) * MS);
  308.  
  309.   TextAttr := Yellow + Blue shl 4; write (' Heap      ');
  310.   Move (Original, Examples, sizeof (Original));
  311.   Started := TimeStamp;
  312.   HeapSort;
  313.   Results ((TimeStamp - Started) * MS);
  314.  
  315.   TextAttr := Yellow + Blue shl 4; write (' Shell     ');
  316.   Move (Original, Examples, sizeof (Original));
  317.   Started := TimeStamp;
  318.   ShellSort;
  319.   Results ((TimeStamp - Started) * MS);
  320.  
  321.   TextAttr := Yellow + Blue shl 4; write (' Fa        ');
  322.   Move (Original, Examples, sizeof (Original));
  323.   Started := TimeStamp;
  324.   TreeSort;
  325.   Results ((TimeStamp - Started) * MS);
  326.  
  327.   TextAttr := Yellow + Blue shl 4; write (' Bincsere  ');
  328.   Move (Original, Examples, sizeof (Original));
  329.   Started := TimeStamp;
  330.   BinExch (1, MaxExample, 1);
  331.   Results ((TimeStamp - Started) * MS);
  332.  
  333.   TextAttr := Yellow + Blue shl 4; write (' Quick     ');
  334.   Move (Original, Examples, sizeof (Original));
  335.   Started := TimeStamp;
  336.   QuickSort (1, MaxExample);
  337.   Results ((TimeStamp - Started) * MS);
  338. end.
  339.