Cod sursa(job #927761)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 26 martie 2013 00:24:23
Problema Sortare prin comparare Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.95 kb
var n:int64;
    v:array[1..500000]of int64;
    i:longint;
    f,g:text;
    buf1,buf2:array[1..1 shl 17]of char;
procedure swap(var aa:int64;var bb:int64);
var aux:int64;
begin
  aux:=aa;
  aa:=bb;
  bb:=aux;
end;
 
procedure quicksort(st, dr:int64);
var
  i, j, pivot, aux,a:int64;
begin
  i := st; j := dr;a:=random(dr-st)+st;
  pivot:= v[a];
  repeat
    while (i < dr) and (v[i] < pivot) do inc(i);
    while (j > st) and (pivot < v[j]) do dec(j);
    if i <= j then
    begin
      if i < j then swap(v[i],v[j]);
      if i < dr then inc(i);
      if j > st then dec(j);
    end
  until i > j;
  if j > st then quicksort(st, j);
  if i < dr then quicksort(i, dr)
end;
 
begin
assign(f,'algsort.in');settextbuf(f,buf1);reset(f);
assign(g,'algsort.out');settextbuf(g,buf2);rewrite(g);
randomize;
readln(f,n);
for i:=1 to n do
  read(f,v[i]);
 
quicksort(1,n);
 
for i:=1 to n do
  write(g,v[i],' ');
close(f);close(g);  
end.