*DECK SPSORT SUBROUTINE SPSORT (X, N, IPERM, KFLAG, IER) C***BEGIN PROLOGUE SPSORT 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 SINGLE 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 SPSORT returns the permutation vector IPERM generated by sorting C the array X and, optionally, rearranges the values in X. X may C be sorted in increasing or decreasing order. A slightly modified C quicksort algorithm is used. C C IPERM is such that X(IPERM(I)) is the Ith value in the rearrangement C of X. IPERM may be applied to another array by calling IPPERM, C SPPERM, DPPERM or HPPERM. C C The main difference between SPSORT and its active sorting equivalent C SSORT is that the data are referenced indirectly rather than C directly. Therefore, SPSORT should require approximately twice as C long to execute as SSORT. However, SPSORT is more general. C C Description of Parameters C X - input/output -- real array of values to be sorted. C If ABS(KFLAG) = 2, then the values in X will be C rearranged on output; otherwise, they are unchanged. C N - input -- number of values in array X 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 X 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 X in increasing order and sort X also. C = 1 means return the permutation vector resulting from C sorting X in increasing order and do not sort X. C = -1 means return the permutation vector resulting from C sorting X in decreasing order and do not sort X. C = -2 means return the permutation vector resulting from C sorting X in decreasing order and sort X 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 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 SPSORT