Cod sursa(job #286391)

Utilizator florin_marius90Florin Marius Popescu florin_marius90 Data 23 martie 2009 19:44:49
Problema Sortare prin comparare Scor 20
Compilator fpc Status done
Runda Arhiva educationala Marime 1.07 kb

type vector=array[1..50] of longint;
var a:vector;
n,ii,m:longint;
f,g:text;


    procedure Sortare(s,d:longint);
      VAR i,j,l1,l2:longint;
          x,z,w:longint;
      begin
        i:=s; j:=d; {x:=(s+d) div 2;}l1:=1; l2:=0  ;
        repeat
          {while a[i]<a[x] do i:=i+1;
          while a[j]>a[x] do j:=j-1;
          if i<=j then begin
            w:=a[i]; a[i]:=a[j]; a[j]:=w;
            i:=i+1; j:=j-1
          end }
        if a[i]>a[j] then begin
                          w:=a[i]; a[i]:=a[j]; a[j]:=w;
                           z:=l1; l1:=-l2; l2:=-z; i:=i+l1; j:=j+l2;
                          end else  
                          begin i:=i+l1; j:=j+l2; end;
       until i=j;
       if s<j-1 then Sortare(s,j-1);
       if d>i then Sortare(i,d);
      end;
    begin
assign(f,'algsort.in'); assign(g,'algsort.out');
reset(f); rewrite(g);

readln(f,n);
for ii:=1 to n do read(f,a[ii]);

      m:=1;
      Sortare(m,n);

for ii:=1 to n do
 write(g,a[ii],' ');
close(f); close(g);

    end.