# dpsort.f

          SUBROUTINE DPSORT (DX, N, IPERM, KFLAG, IER)
    C***BEGIN PROLOGUE  DPSORT
    C***PURPOSE  Return the permutation vector generated by sorting a given
    C            array and, optionally, rearrange the elements of the array.
    C            The array may be sorted in increasing or decreasing order.
    C            A slightly modified quicksort algorithm is used.
    C***LIBRARY   SLATEC
    C***CATEGORY  N6A1B, N6A2B
    C***TYPE      DOUBLE PRECISION (SPSORT-S, DPSORT-D, IPSORT-I, HPSORT-H)
    C***KEYWORDS  NUMBER SORTING, PASSIVE SORTING, SINGLETON QUICKSORT, SORT
    C***AUTHOR  Jones, R. E., (SNLA)
    C           Rhoads, G. S., (NBS)
    C           Wisniewski, J. A., (SNLA)
    C***DESCRIPTION
    C
    C   DPSORT returns the permutation vector IPERM generated by sorting
    C   the array DX and, optionally, rearranges the values in DX.  DX may
    C   be sorted in increasing or decreasing order.  A slightly modified
    C   quicksort algorithm is used.
    C
    C   IPERM is such that DX(IPERM(I)) is the Ith value in the
    C   rearrangement of DX.  IPERM may be applied to another array by
    C   calling IPPERM, SPPERM, DPPERM or HPPERM.
    C
    C   The main difference between DPSORT and its active sorting equivalent
    C   DSORT is that the data are referenced indirectly rather than
    C   directly.  Therefore, DPSORT should require approximately twice as
    C   long to execute as DSORT.  However, DPSORT is more general.
    C
    C   Description of Parameters
    C      DX - input/output -- double precision array of values to be
    C           sorted.  If ABS(KFLAG) = 2, then the values in DX will be
    C           rearranged on output; otherwise, they are unchanged.
    C      N  - input -- number of values in array DX to be sorted.
    C      IPERM - output -- permutation array such that IPERM(I) is the
    C              index of the value in the original order of the
    C              DX array that is in the Ith location in the sorted
    C              order.
    C      KFLAG - input -- control parameter:
    C            =  2  means return the permutation vector resulting from
    C                  sorting DX in increasing order and sort DX also.
    C            =  1  means return the permutation vector resulting from
    C                  sorting DX in increasing order and do not sort DX.
    C            = -1  means return the permutation vector resulting from
    C                  sorting DX in decreasing order and do not sort DX.
    C            = -2  means return the permutation vector resulting from
    C                  sorting DX in decreasing order and sort DX also.
    C      IER - output -- error indicator:
    C          =  0  if no error,
    C          =  1  if N is zero or negative,
    C          =  2  if KFLAG is not 2, 1, -1, or -2.
    C***REFERENCES  R. C. Singleton, Algorithm 347, An efficient algorithm
    C                 for sorting with minimal storage, Communications of
    C                 the ACM, 12, 3 (1969), pp. 185-187.
    C***ROUTINES CALLED  XERMSG
    C***REVISION HISTORY  (YYMMDD)
    C   761101  DATE WRITTEN
    C   761118  Modified by John A. Wisniewski to use the Singleton
    C           quicksort algorithm.
    C   870423  Modified by Gregory S. Rhoads for passive sorting with the
    C           option for the rearrangement of the original data.
    C   890619  Double precision version of SPSORT created by D. W. Lozier.
    C   890620  Algorithm for rearranging the data vector corrected by R.
    C           Boisvert.
    C   890622  Prologue upgraded to Version 4.0 style by D. Lozier.
    C   891128  Error when KFLAG.LT.0 and N=1 corrected by R. Boisvert.
    C   920507  Modified by M. McClain to revise prologue text.
    C   920818  Declarations section rebuilt and code restructured to use
    C           IF-THEN-ELSE-ENDIF.  (SMR, WRB)
    C***END PROLOGUE  DPSORT