FNLMath : Utilities : SVIGP
SVIGP
Sorts an integer array by algebraically increasing value and return the permutation that rearranges the array.
Required Arguments
IA — Integer vector of length N containing the array to be sorted. (Input)
IB — Integer vector of length N containing the sorted array. (Output)
If IA is not needed, IA and IB 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 IA.
Optional Arguments
N — Number of elements in the array to be sorted. (Input)
Default: N = size (IPERM,1).
FORTRAN 90 Interface
Generic: CALL SVIGP (IA, IB, IPERM [])
Specific: The specific interface name is S_SVIGP.
FORTRAN 77 Interface
Single: CALL SVIGP (N, IA, IB, IPERM)
Description
Routine SVIGP sorts the elements of an integer 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 SVIGP uses the algorithm discussed in SVRGN. On completion, Aj  Ai for j < i.
1. For wider applicability, integers (1, 2, N) that are to be associated with IA(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 IA algebraically.

USE SVIGP_INT
USE UMACH_INT

IMPLICIT NONE
! Declare variables
INTEGER N, J, NOUT
PARAMETER (N=10)
INTEGER IA(N), IB(N), IPERM(N)
! Set values for IA and IPERM
! IA = ( 10 -9 8 -7 6 5 4 -3 -2 -1 )
!
! IPERM = ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
!
DATA IA/10, -9, 8, -7, 6, 5, 4, -3, -2, -1/
DATA IPERM/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/
! Sort IA by algebraic value into IB
CALL SVIGP (IA, IB, IPERM)
! Print results
CALL UMACH (2,NOUT)
WRITE (NOUT, 99998) (IB(J),J=1,N)
WRITE (NOUT, 99999) (IPERM(J),J=1,N)
!
99998 FORMAT (' The output vector is:', /, 10(1X,I5))
99999 FORMAT (' The permutation vector is:', /, 10(1X,I5))
END
Output

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

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