program teste_ordena;
 procedure troca (var x, y: integer);
 var
    aux	: integer;
 begin
    aux := x;
    x := y;
    y := aux;
 end;

procedure ordena (var a, b, c, d : integer);
begin
   {Passo 1: garante que a contém o menor valor.}
   if (b < a) and (b <= c) and (b <= d) then
      troca (a, b)
   else
      if (c < a) and (c <= b) and (c <= d) then
	 troca (a, c)
      else
	 if (d < a) and (d <= b) and (d <= c) then
	    troca (a, d);

   {Passo 2: garante que b contém o segundo menor valor.}
   if (c < b) and (c <= d) then
      troca (b, c)
   else
      if (d < b) and (d <= c) then
	 troca (b, d);

   {Passo 3: garante que c contém o terceiro menor valor e
   que d contém o maior valor. }

   if (c > d) then
      troca (c, d);
end; 

var
   a, b, c, d: integer;
begin 
   write('a: '); read(a);
   write('b: '); read(b);
   write('c: '); read(c); 
   write('d: '); read(d);  
   ordena(a,b,c,d);
   writeln('Os valores de a, b, c e d foram ordenados. ');
   writeln('a: ', a, ' b: ', b,' c: ', c, ' d: ', d); 
end.
