*" PACKAGE INDEXX :" 小さい順番号付け
*
*"  [HIS] 92/12/17(takepiro) Numerical Recipies より移植
*
*"   大きさで並び変えるには INDX(N) を用いて次のようにすればよい
*
*         DO 100 I = 1, N
*     100    WORK( I ) = ARRAY( I )
*
*         DO 200 I = 1, N
*     200    ARRAY( I ) = WORK( INDX(I) )       !" 小さい順
*         DO 200 I = 1, N
*     200    ARRAY( I ) = WORK( INDX(N-I+1) )   !" 大きい順
*
**********************************************************************
      SUBROUTINE INDEXX
     I             ( N    , ARRIN ,
     O               INDX            )
*
*"  [INPUT]
      INTEGER  N                      !" 配列の大きさ
      REAL     ARRIN( N )             !" 番号付けする配列
*
*"  [OUTPUT]
      INTEGER  INDX ( N )             !" 番号の入った配列
*
*"  [INTERNAL WORK]
      INTEGER  IR, L, I, J, INDXT
      REAL     Q
*
      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
