BEGIN
COMMENT ALGOL-M PROGRAM TO SORT DECIMAL NUMBERS IN DESCENDING
        ORDER :- INPUT IS FROM THE CONSOLE;
INTEGER N;
 
PROCEDURE HEADING(N);
  INTEGER N;
  WRITE("DATA SET",N);
 
PROCEDURE DASHES(N);
  INTEGER N;
  BEGIN INTEGER I;
    WRITE("-"); FOR I:=2 STEP 1 UNTIL N DO WRITEON("-");
  END;
 
PROCEDURE TITLE;
  BEGIN
    WRITE(" ALGOL-M SORTING PROGRAM");
    WRITE(" "); WRITE(" ");
  END;
 
PROCEDURE INPUT;
  BEGIN
   WRITE("INPUT AN INTEGER TO INDICATE THE NUMBER OF DECIMAL");
   WRITE("NUMBERS TO BE SORTED.  INPUT 9999 TO TERMINATE ");
   READ(N);
 END;
INTEGER COUNT,K; COUNT:=0; K:=30;
TITLE; INPUT;
 WHILE N<>9999 DO
  BEGIN
    DECIMAL ARRAY SORT[1:N];
 
    INTEGER FUNCTION SORTER;
      BEGIN INTEGER I,COUNT,SORTED;
            DECIMAL T;
      COUNT:=1; SORTED:=0; T:=1.0;
      WHILE COUNT<>0 DO
        BEGIN COUNT:=0;
        FOR I:=1 STEP 1 UNTIL N-1 DO
          BEGIN IF SORT[I]<SORT[I+1] THEN
                  BEGIN
                    COUNT:=COUNT+1;
                    SORTED:=SORTED+1;
                    T:=SORT[I]; SORT[I]:=SORT[I+1]; SORT[I+1]:=T;
                  END;
          END;
        END;
    SORTER:=SORTED;
      END;
 
PROCEDURE PRINT;
   BEGIN INTEGER I;
     FOR I:=1 STEP 1 UNTIL N DO WRITE(I," ",SORT[I]);
   END;
 
PROCEDURE READER;
   BEGIN INTEGER I;
     WRITE("INPUT DECIMAL NUMBERS");
     FOR I:=1 STEP 1 UNTIL N DO READ(SORT[I]);
     PRINT;
   END;
 
DASHES(K); HEADING((COUNT:=COUNT+1)); DASHES(K);
WRITE(" "); WRITE(" ");
WRITE("ORIGINAL DATA ORDER"); READER;
WRITE(" ");
WRITE("THERE WERE", SORTER," EXCHANGES REQUIRED; RESULTING ORDER IS");
PRINT; DASHES(K); WRITE(" "); INPUT;
END;
WRITE(" "); WRITE("END OF RUN");
END
EOF
