c csort3a.f c c
      
	SUBROUTINE SSORT (X, IY, N, KFLAG)
      IMPLICIT NONE
c
c    Example of an Insertion Sort, Modified to Fortran90
c    Function in shifting contents of X
c
C***BEGIN PROLOGUE  SSORT
C***PURPOSE  Sort an array and make the same interchanges in
C            an auxiliary array.  The array is sorted in
C            decreasing order.
C***TYPE      SINGLE PRECISION
C***KEYWORDS  SORT, SORTING
C
C   Description of Parameters
C      X - array of values to be sorted   (usually abscissas)
C      IY - array to be carried with X (all swaps of X elements are
C          matched in IY .  After the sort IY(J) contains the original
C          postition of the value X(J) in the unsorted X array.
C      N - number of values in array X to be sorted
C      KFLAG - Not used in this implementation
C
C***REVISION HISTORY  (YYMMDD)
C   950310  DATE WRITTEN
C   John Mahaffy
C***END PROLOGUE  SSORT
C     .. Scalar Arguments ..
      INTEGER KFLAG, N
C     .. Array Arguments ..
      REAL X(*)
      INTEGER IY(*)
C     .. Local Scalars ..
      REAL TEMP
      INTEGER I, J, K, ITEMP
C     .. External Subroutines ..
C     None
C     .. Intrinsic Functions ..
C     None
C
C***FIRST EXECUTABLE STATEMENT  SSORT
C
      DO 100 I=2,N
c
c    If the Ith element is out of order with the preceeding
c    element search for its position in the portion of the
c    array that is already ordered.
c
         IF ( X(I).GT.X(I-1) ) THEN
            DO 50 J=I-2,1,-1
              IF(X(I).LT.X(J)) go to 70
  50          CONTINUE
            J=0
c
c     Use Fortran 90 Intrinsic Function to
c     Shift   array elements and insert
c     cshift is a circular shift.  With -1 as the second
c     argument, it shifts all listed array elements up one
c     element in the array, except the last listed element, which
c     circles around to the first listed array element.
c
c
  70        x(j+1:i) = cshift(x(j+1:i),-1)
            iy(j+1:i) = cshift(iy(j+1:i),-1)
c
         ENDIF
  100 CONTINUE
      RETURN
      END
c
c c