SYSTEM BubbleSort;
CONST   size=7;
Type  rind = array [0..size-1] of integer;

CONFIGURATION  field[1..size];
CONNECTION right: field[i] <-> field[i+1].left;
                      
SCALAR  i,a      : INTEGER;
        t: rind;
VECTOR  value,aux, 
        buffer : INTEGER;



Procedure Afisare(Scalar t: rind);
Scalar j:integer;
Begin
 WriteLn;
 For j:=0 to size-1 do WriteInt(t[j], 3); End (* for *);
 WriteLn;
End Afisare;
  



BEGIN 
  PARALLEL
    value := size - id_no; 
  ENDPARALLEL;
  Store(value, t);
  Afisare(t);
  ReadInt(a); 
  FOR i := 1 TO (size DIV 2)+1  DO
    PARALLEL
       buffer := value;
       PROPAGATE.left(buffer);

	(* lucreaza procesoarele impare *)
       IF ((dim1 MOD 2) = 1) THEN
            IF (buffer < value) THEN (*switch buffer<->value*) 
	      aux:=buffer; buffer:=value; value:=aux;
            END; (*if*)
       END; 
       PROPAGATE.right(buffer);
       IF ((dim1 MOD 2) = 0) THEN value:=buffer; END;

WriteInt(value,3);
ReadInt(a);

	(* lucreaza procesoarele pare *)
    	buffer := value;
      PROPAGATE.left(buffer);
      IF ((dim1 MOD 2) = 0) THEN 
          IF (buffer < value) THEN (*switch buffer<->value*) 
	      aux:=buffer; buffer:=value; value:=aux;
          END; (*if*)
      END; (*if*)
      PROPAGATE.right(buffer); 
      IF ( ((dim1 MOD 2) = 1 ) AND (dim1 > 1) ) THEN value:=buffer; END;
    ENDPARALLEL;
    Store(value, t);
    Afisare(t);  
    ReadInt(a);
  END ; (* FOR *)


END BubbleSort.


