FNLMath : Utilities : SVRGP
SVRGP
Sorts a real array by algebraically increasing value and return the permutation that rearranges the array.
Required Arguments
RA — Vector of length N containing the array to be sorted. (Input)
RB — Vector of length N containing the sorted array. (Output)
If RA is not needed, RA and RB can share the same storage locations.
IPERM — Vector of length N. (Input/Output)
On input, IPERM should be initialized to the values 1, 2, , N. On output, IPERM contains a record of permutations made on the vector RA.
Optional Arguments
N — Number of elements in the array to be sorted. (Input)
Default: N = size (IPERM,1).
FORTRAN 90 Interface
Generic: CALL SVRGP (RA, RB, IPERM [])
Specific: The specific interface names are S_SVRGP and D_SVRGP.
FORTRAN 77 Interface
Single: CALL SVRGP (N, RA, RB, IPERM)
Double: The double precision name is DSVRGP.
Description
Routine SVRGP sorts the elements of an array, A, into ascending order by algebraic value, keeping a record in P of the permutations to the array A. That is, the elements of P are moved in the same manner as are the elements in A as A is being sorted. The routine SVRGP uses the algorithm discussed in SVRGN. On completion, Aj  Ai for j < i.
Comments
1. For wider applicability, integers (1, 2, …, N) that are to be associated with RA(I) for I = 1, 2, N may be entered into IPERM(I) in any order. Note that these integers must be unique.
Example
This example sorts the 10-element array RA algebraically.

USE SVRGP_INT
USE UMACH_INT

IMPLICIT NONE
! Declare variables
INTEGER N, NOUT, J
PARAMETER (N=10)
REAL RA(N), RB(N)
INTEGER IPERM(N)
! Set values for RA and IPERM
! RA = ( 10.0 -9.0 8.0 -7.0 6.0 5.0 4.0 -3.0 -2.0 -1.0 )
!
! IPERM = ( 1 2 3 4 5 6 7 8 9 10)
!
DATA RA/10.0, -9.0, 8.0, -7.0, 6.0, 5.0, 4.0, -3.0, -2.0, -1.0/
DATA IPERM/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/
! Sort RA by algebraic value into RB
CALL SVRGP (RA, RB, IPERM)
! Print results
CALL UMACH (2,NOUT)
WRITE (NOUT, 99998) (RB(J),J=1,N)
WRITE (NOUT, 99999) (IPERM(J),J=1,N)
!
99998 FORMAT (' The output vector is:', /, 10(1X,F5.1))
99999 FORMAT (' The permutation vector is:', /, 10(1X,I5))
END
Output

The output vector is:
-9.0 -7.0 -3.0 -2.0 -1.0 4.0 5.0 6.0 8.0 10.0

The permutation vector is:
2 4 8 9 10 7 6 5 3 1