      SUBROUTINE MCSORT_INSERT(INDX,NMAX,IVAL,IADR,N,IER)
C
      INTEGER INDX(NMAX)	! SORT ARRAY
C
C  INSERT VALUE IVAL AT ADDRESS IADR IN INDX ARRAY
C  INCREASE THE NUMBER N OF ELEMENTS RECORDED AS STORED IN INDX.
C  RETURN IER=1 IF N IS ALREADY .GE.NMAX
C
      IF(N.GE.NMAX) THEN
C  NUMBER CHECK
        IER=1
      ELSE IF((IADR.LT.1).OR.(IADR.GT.(N+1))) THEN
C  ADDRESS RANGE CHECK
        IER=2
      ELSE
        IER=0
        IF(IADR.EQ.(N+1)) THEN
C  STICK AT END
          INDX(IADR)=IVAL
        ELSE
C  MAKE ROOM FOR INSERTION
          DO 10 I=N,IADR,-1
            IP1=I+1
            INDX(IP1)=INDX(I)
 10       CONTINUE
C  INSERT
          INDX(IADR)=IVAL
        ENDIF
C  INCREMENT NUMBER
        N=N+1
      ENDIF
C
      RETURN
      END
