SUBROUTINE IPSORT (IX, N, IPERM, KFLAG, IER)
C***BEGIN PROLOGUE IPSORT
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***CATEGORY N6A1A, N6A2A
C***TYPE INTEGER (SPSORT-S, DPSORT-D, IPSORT-I, HPSORT-H)
C***KEYWORDS NUMBER SORTING, PASSIVE SORTING, SINGLETON QUICKSORT, SORT
C***AUTHOR Jones, R. E., (SNLA)
C Kahaner, D. K., (NBS)
C Rhoads, G. S., (NBS)
C Wisniewski, J. A., (SNLA)
C IPSORT returns the permutation vector IPERM generated by sorting
C the array IX and, optionally, rearranges the values in IX. IX may
C be sorted in increasing or decreasing order. A slightly modified
C quicksort algorithm is used.
C IPERM is such that IX(IPERM(I)) is the Ith value in the
C rearrangement of IX. IPERM may be applied to another array by
C calling IPPERM, SPPERM, DPPERM or HPPERM.
C The main difference between IPSORT and its active sorting equivalent
C ISORT is that the data are referenced indirectly rather than
C directly. Therefore, IPSORT should require approximately twice as
C long to execute as ISORT. However, IPSORT is more general.
C Description of Parameters
C IX - input/output -- integer array of values to be sorted.
C If ABS(KFLAG) = 2, then the values in IX will be
C rearranged on output; otherwise, they are unchanged.
C N - input -- number of values in array IX 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 IX array that is in the Ith location in the sorted
C KFLAG - input -- control parameter:
C = 2 means return the permutation vector resulting from
C sorting IX in increasing order and sort IX also.
C = 1 means return the permutation vector resulting from
C sorting IX in increasing order and do not sort IX.
C = -1 means return the permutation vector resulting from
C sorting IX in decreasing order and do not sort IX.
C = -2 means return the permutation vector resulting from
C sorting IX in decreasing order and sort IX 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 810801 Further modified by David K. Kahaner.
C 870423 Modified by Gregory S. Rhoads for passive sorting with the
C option for the rearrangement of the original data.
C 890620 Algorithm for rearranging the data vector corrected by R.
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 IPSORT