Program SimpleVstavka(input,output);
uses crt;
Type Data=array[1..3] of integer;
Var a,b:array[0..100] of Data;
    ssr,spr:array[1..2,1..6] of integer;
    sumsravn,sumperem:array[1..5] of integer;
    n:integer;
Function sravnenie(i,j,l:integer):boolean;
Begin
 if ((a[i][3]<a[j][3]) or ((a[j][3]=a[i][3]) and (a[i][2]<a[j][2]))
     or ((a[j][3]=a[i][3]) and ((a[j][2]=a[i][2]) and (a[i][1]<a[j][1])))) then
  sravnenie:=true
 else
  sravnenie:=false;
 sumsravn[l]:=sumsravn[l]+1;
End;
Procedure priamfill(n:integer);
Var i:integer;
Begin
for i:=1 to (n-1) do
 begin
  a[i][3]:=i;
  a[i][2]:=1;
  a[i][1]:=1;
 end;
 a[n][3]:=99;
 a[n][2]:=1;
 a[n][1]:=2;
end;
Procedure obratfill(n:integer);
Var i:integer;
Begin
 for i:=1 to (n-1) do
  begin
   a[i][3]:=n-i;
   a[i][2]:=1;
   a[i][1]:=2;
  end;
  a[n][3]:=1;
  a[n][2]:=1;
  a[n][1]:=1;
End;
Procedure smeshfill(n:integer);
Var i:integer;
Begin
 for i:=1 to (n-1) do
  if odd(i) then
   begin
    a[i][3]:=n-i;
    a[i][2]:=1;
    a[i][1]:=1;
   end
  else
   begin
    a[i][3]:=i;
    a[i][2]:=1;
    a[i][1]:=1;
   end;
  a[n][3]:=n-i;
  a[n][2]:=1;
  a[n][1]:=2;
End;
Procedure randomfill(n:integer);
Var i:integer;
Begin
 randomize;
 for i:=1 to n do
  begin
   a[i][2]:=round(random(12));
   a[i][3]:=round(random(99));
   case a[i][2] of
    0,1,3,5,7,8,10,12:
      a[i][1]:=random(31);
    4,6,9,11:
      a[i][1]:=random(30);
    2:
      if ((a[i][3] mod 4)=0) then
       a[i][1]:=random(29)
      else
       a[i][1]:=random(28);
    end;
   Write(a[i][1],'.',a[i][2],'.',a[i][3],'   ')
  end;
End;
procedure Simplvstavka(n,l:integer);
Var i,j,k:integer;
    d:data;
Begin
 sumsravn[l]:=0;
 sumperem[l]:=0;
 For i:=2 to n do
  Begin
   for j:=1 to (i-1) do
    begin
     sumsravn[l]:=sumsravn[l]+1;
     if sravnenie(i,j,l) then
      for k:=i downto (j+1) do
       begin
        sumperem[l]:=sumperem[l]+1;
        a[0]:=a[k];
        a[k]:=a[k-1];
        a[k-1]:=a[0];
       end;
    end;
  End;
end;
Procedure Estestvslianie(n,l:integer);
var i,j,k:integer;
Begin
 repeat
  j:=1;
  k:=n;
  sumsravn[l]:=0;
  sumperem[l]:=0;
  repeat
   while (sravnenie(i,j,l) and (j<>n)) do
    begin
     sumperem[l]:=sumperem[l]+1;
     b[j]:=a[j];
     j:=j+1;
    end;
   while sravnenie(i,j,l) do
    k:=k-1;
   for i:=k to n do
    begin
     b[i]:=a[n-i+k];
     sumperem[l]:=sumperem[l]+1;
    end;
  until j<>k;
  j:=1;
  k:=n;
  repeat
   while sravnenie(i,j,l) do
   begin
    a[j]:=b[j];
    j:=j+1;
    sumperem[l]:=sumperem[l]+1;
   end;
  while sravnenie(k,i,l) do
   k:=k-1;
  for i:=k to n do
   begin
    a[i]:=b[n-i+k];
    sumperem[l]:=sumperem[l]+1;
   end;
   until j<>k;
 until j<>n;
End;
Procedure Sravn(name:string);
Var i,j:integer;
Begin
  writeln('                 ', ' name ' , '                 ');
  writeln(' ---------------------------------------------------------------');
  writeln(' |  n  |  ¯ à ¬¥âà   |  ­®¬¥à ¯®á«¥¤®¢ â¥«ì­®áâ¨  |  áà¥¤­¥¥   |');
  writeln(' |     |             |    1    2    3    4    5   |  §­ ç¥­¨¥  |');
  writeln(' ---------------------------------------------------------------');
   for i:=1 to 4 do
    begin
     case i of
      1:n:=10;
      2:n:=20;
      3:n:=50;
      4:n:=100;
     end;
    write(' | ', n , ' | áà ¢­¥­¨ï   | ');
    for j:=1 to 5 do
     begin
      case j of
       1:priamfill(n);
       2:obratfill(n);
       3:smeshfill(n);
       4:randomfill(n);
       5:randomfill(n);
      end;
      if name='simplvstavka' then
       simplvstavka(n,j)
      else
       estestvslianie(n,j);
     end;
     writeln(sumsravn[1]:4, ' ',sumsravn[2]:4, ' ',sumsravn[3]:4, ' ',sumsravn[4]:4, ' ',
            sumsravn[5]:4, '   |   ',(round((sumsravn[1]+sumsravn[2]+sumsravn[3]+sumsravn[4]+sumsravn[5])/5)):4, '     |');
     writeln(' |     | ¯¥à¥¬¥é¥­¨ï | ',
            sumperem[1]:4, ' ',sumperem[2]:4, ' ',sumperem[3]:4, ' ',sumperem[4]:4, ' ',
            sumperem[5]:4, '   |   ',(round((sumperem[1]+sumperem[2]+sumperem[3]+sumperem[4]+sumperem[5])/5)):4, '     |');
     writeln(' ---------------------------------------------------------------');
    end;
End;
Begin
 clrscr;
 sravn('simplvstavka');
 sravn('estestvslijinie');
end.
