      SUBROUTINE INDEXX(N,ARRIN,INDX)
      DIMENSION ARRIN(N),INDX(N)
      DO 11 J=1,N
        INDX(J)=J
11    CONTINUE
      L=N/2+1
      IR=N
10    CONTINUE
        IF(L.GT.1)THEN
          L=L-1
          INDXT=INDX(L)
          Q=ARRIN(INDXT)
        ELSE
          INDXT=INDX(IR)
          Q=ARRIN(INDXT)
          INDX(IR)=INDX(1)
          IR=IR-1
          IF(IR.EQ.1)THEN
            INDX(1)=INDXT
            RETURN
          ENDIF
        ENDIF
        I=L
        J=L+L
20      IF(J.LE.IR)THEN
          IF(J.LT.IR)THEN
            IF(ARRIN(INDX(J)).LT.ARRIN(INDX(J+1)))J=J+1
          ENDIF
          IF(Q.LT.ARRIN(INDX(J)))THEN
            INDX(I)=INDX(J)
            I=J
            J=J+J
          ELSE
            J=IR+1
          ENDIF
        GO TO 20
        ENDIF
        INDX(I)=INDXT
      GO TO 10
      END
      FUNCTION IRBIT1(ISEED)
      LOGICAL NEWBIT
      PARAMETER (IB1=1,IB2=2,IB5=16,IB18=131072)
      NEWBIT=IAND(ISEED,IB18).NE.0
      IF(IAND(ISEED,IB5).NE.0)NEWBIT=.NOT.NEWBIT
      IF(IAND(ISEED,IB2).NE.0)NEWBIT=.NOT.NEWBIT
      IF(IAND(ISEED,IB1).NE.0)NEWBIT=.NOT.NEWBIT
      IRBIT1=0
      ISEED=IAND(ISHFT(ISEED,1),NOT(IB1))
      IF(NEWBIT)THEN
        IRBIT1=1
        ISEED=IOR(ISEED,IB1)
      ENDIF
      RETURN
      END
      FUNCTION IRBIT2(ISEED)
      PARAMETER (IB1=1,IB2=2,IB5=16,IB18=131072,MASK=IB1+IB2+IB5)
      IF(IAND(ISEED,IB18).NE.0)THEN
        ISEED=IOR(ISHFT(IEOR(ISEED,MASK),1),IB1)
        IRBIT2=1
      ELSE
        ISEED=IAND(ISHFT(ISEED,1),NOT(IB1))
        IRBIT2=0
      ENDIF
      RETURN
      END
