Cod sursa(job #286091)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 23 martie 2009 14:16:03
Problema Sortare prin comparare Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.83 kb
type vector=array[1..500000] of longint;
var a:vector;
n,i,m:longint;
f,g:text;

function partition(s,d:longint) : longint ;
	VAR aux, piv, i, j : longint;
begin
	piv := a[(s + d) div 2];
        i := s - 1; j := d + 1;
	while true do begin
		repeat inc(i) until a[i] >= piv;
  		repeat dec(j) until a[j] <= piv;
  		if i < j then begin
    			aux := a[i]; a[i] := a[j]; a[j] := aux;
  		end
  		else begin
  			partition := j;
  			exit;
  		end
	end
end;

procedure Sortare(s,d:longint);
	VAR m : longint;
begin
	if (s < d) then begin
		m := partition(s, d);
		Sortare(s, m);
		Sortare(m + 1, d);
	end;
end;
   
begin
	assign(f,'algsort.in'); assign(g,'algsort.out');
	reset(f); rewrite(g);
	
	readln(f,n);
	for i := 1 to n do read(f,a[i]);
	
	Sortare(1,n);

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

end.