      SUBROUTINE SHELL(A,N)
      REAL A(N)
C     ------------------------------------------------------------------
C     THE SHELL SORTING PROCEDURE IS USED TO REORDER THE ELEMENTS OF A
C     SO THAT A(I).LE.A(I+1) FOR I=1,...,N-1. IT IS ASSUMED THAT N.GE.1.
C     ------------------------------------------------------------------
      INTEGER K(10)
      DATA K(1)/1/, K(2)/4/, K(3)/13/, K(4)/40/, K(5)/121/, K(6)/364/,
     1     K(7)/1093/, K(8)/3280/, K(9)/9841/, K(10)/29524/
C
C             SELECTION OF THE INCREMENTS K(I) = (3**I-1)/2
C
      IF (N .LT. 2) RETURN
      IMAX = 1
      DO 10 I=3,10
      IF (N .LE. K(I)) GO TO 20
   10 IMAX = IMAX + 1
C
C            STEPPING THROUGH THE INCREMENTS K(IMAX),...,K(1)
C
   20 I = IMAX
      DO 40 II=1,IMAX
      KI = K(I)
C
C             SORTING ELEMENTS THAT ARE KI POSITIONS APART
C               SO THAT A(J).LE.A(J+KI) FOR J=1,...,N-KI
C
      JMAX = N - KI
      DO 31 J=1,JMAX
      L = J
      LL = J + KI
      S = A(LL)
   30 IF (S .GE. A(L)) GO TO 31
      A(LL) = A(L)
      LL = L
      L = L - KI
      IF (L .GT. 0) GO TO 30
   31 A(LL) = S
C
   40 I = I - 1
      RETURN
      END
