C....*...1.........2.........3.........4.........5.........6.........7.*.......8
C     DSORT2   4/30/75
C
C     PURPOSE
C     SORT EACH COLUMN OF A MATRIX B ON THE VECTOR A.
C
C     USAGE
C     CALL DSORT2(A,B,N,K)
C
C     ARGUMENTS
C     A - INPUT VECTOR OF LENGTH N.  ON EXIT A HAS BEEN SORTED INTO
C         ASCENDING SEQUENCE.  A CANNOT BE A COLUMN OF B.
C         REAL*8
C     B - AN N BY K MATRIX.  ON EXIT THE ROWS OF B ARE REORDERED TO
C         CORRESPOND TO THE REORDERING OF THE VECTOR A.
C         REAL*8
C     N - NUMBER OF ROWS IN A AND B.
C         INTEGER*4
C     K - NUMBER OF COLUMNS IN B.
C         INTEGER*4
C
C
C
      SUBROUTINE DSORT2(A,B,N,K)
      IMPLICIT REAL*8 (A-H,O-Z)
      save
      REAL*8 A(N),B(N,K)
      M=N
20    M=M/2
      IF(M)30,40,30
30    KK=N-M
      J=1
41    I=J
49    L=I+M
      IF(A(I)-A(L))60,60,50
50    R=A(I)
      A(I)=A(L)
      A(L)=R
      DO 55 JJ=1,K
      R=B(I,JJ)
      B(I,JJ)=B(L,JJ)
55    B(L,JJ)=R
      I=I-M
      IF(I-1)60,49,49
60    J=J+1
      IF(J-KK)41,41,20
40    CONTINUE
      RETURN
      END
