Cod sursa(job #408501)

Utilizator hungntnktpHungntnktp hungntnktp Data 3 martie 2010 05:03:53
Problema Sortare prin comparare Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.73 kb
const fi        =       'sort.in';
      fo        =       'sort.out';
      maxn      =       555555;
var a : array[0..maxn] of longint;
    n,i : longint;
    tf : text;

procedure sort(lo,hi : longint);
Var i,j,x,y : longint;
 begin
  i := lo; j := hi; x := a[random(j-i-1)+1+i];
  Repeat
   while a[i] < x do inc(i);
   while a[j] > x do dec(j);
   If i <= j then
    begin
     y := a[i]; a[i] := a[j]; a[j] := y;
     inc(i); dec(j);
    end;
  until i > j;
  if hi > i then sort(i,hi);
  if j > lo then sort(lo,j);
 end;
begin
 assign(tf,fi);
 reset(tf);
 read(tf,n);
 For i := 1 to n do Read(tf,a[i]);
 close(tf);
 assign(tf,fo);
 rewrite(tf);
 For i := 1 to n do Write(tf,a[i],' ');
 close(tf);

end.