Cod sursa(job #694422)

Utilizator boby301Bogdan Bacila boby301 Data 27 februarie 2012 21:00:40
Problema Sortare prin comparare Scor 20
Compilator fpc Status done
Runda Arhiva educationala Marime 0.83 kb
var n:int64;
    v:array[1..100]of int64;
    i:longint;
    f,g:text;
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');reset(f);
assign(g,'algsort.out');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.