Cod sursa(job #946043)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 3 mai 2013 18:07:55
Problema Sortare prin comparare Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.88 kb
program qsort;
  var n,i:longint;
       a:array [1..1000000] of longint;
       bufin,bufout:array[1..100000] of byte;

procedure quicksort(l,r:longint);
  var x,i,j,y:longint;
  begin
    x:=a[(r+l)div 2];
    i:=l;j:=r;
    repeat
      begin
        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;
            dec(j);
            inc(i);
          end;
      end;
    until i>j ;
    if l<j then quicksort(l,j);
    if r> i then quicksort(i,r);
  end;


begin
  assign(input,'algsort.in');
  reset(input);
  settextbuf(input,bufin);
  assign(output,'algsort.out');
  rewrite(output);
  settextbuf(output,bufout);
  readln(n);
  for i:=1 to n do read(a[i]);
  quicksort(1,n);
  for i:=1 to n do write(a[i],' ');
  close(input);close(output);
end.