*     **********************
      SUBROUTINE SORT(A,L,N)
*     **********************
*
*     The subroutine SORT applies to the array A with J elements.
*     The indices of the ordered array are placed in array L to
*     obtain the ordered array replace the indices I of A(I) with
*     I = L(I) - REF. CACM 271.
*
      DIMENSION A(300),L(300),IUT(10),ILT(10)
      J=N
      I=1
      M=1
      DO 10 K=1,J
   10 L(K)=K
   20 IF(J.LE.I-1) GO TO 60
      IP=(J+I)/2
      ITT=L(IP)
      T=A(ITT)
      L(IP)=L(I)
      IQ=J
      K=I+1
   30 IF(K.GT.IQ) GO TO 50
      LK=L(K)
      IF(A(LK).LE.T) THEN
         K=K+1
         GO TO 30
      END IF
   40 IF(IQ.LT.K) THEN
         IQ=K-1
         GO TO 50
      END IF
      LQ=L(IQ)
      IF(A(LQ).LT.T) THEN
         IX=L(K)
         L(K)=L(IQ)
         L(IQ)=IX
         IQ=IQ-1
         K=K+1
         GO TO 30
      END IF
      IQ=IQ-1
      GO TO 40
   50 L(I)=L(IQ)
      L(IQ)=ITT
      IF(2*IQ.GT.I-J) THEN
         ILT(M)=I
         IUT(M)=IQ-1
         I=IQ+1
      ELSE
         ILT(M)=IQ+1
         IUT(M)=J
         J=IQ-1
      END IF
      M=M+1
      GO TO 20
   60 IF(I.LT.J) THEN
         LI=L(I)
         LJ=L(J)
         IF(A(LI).GT.A(LJ)) THEN
            IX=L(I)
            L(I)=L(J)
            L(J)=IX
         END IF
      END IF
      M=M-1
      IF(M.GT.0) THEN
         I=ILT(M)
         J=IUT(M)
         GO TO 20
      END IF
      RETURN
      END
