FNLMath : Utilities : SORT_REAL
SORT_REAL
Sorts a rank-1 array of real numbers x so the y results are algebraically nondecreasing, y1  y2   yn.
Required Arguments
X — Rank-1 array containing the numbers to be sorted. (Output)
Y — Rank-1 array containing the sorted numbers. (Output)
Optional Arguments
nsize = n (Input)
Uses the sub-array of size n for the numbers.
Default value: n = size(x)
iperm = iperm (Input/Output)
Applies interchanges of elements that occur to the entries of iperm(:). If the values iperm(i)=i,i=1,n are assigned prior to call, then the output array is moved to its proper order by the subscripted array assignment y = x(iperm(1:n)).
icycle = icycle (Output)
Permutations applied to the input data are converted to cyclic interchanges. Thus, the output array y is given by the following elementary interchanges, where :=: denotes a swap:
j = icycle(i)
y(j) :=: y(i), i = 1,n
iopt = iopt(:) (Input)
Derived type array with the same precision as the input matrix; used for passing optional data to the routine. The options are as follows:
Packaged Options for SORT_REAL
Option Prefix = ?
Option Name
Option Value
s_, d_
Sort_real_scan_for_NaN
1
iopt(IO) = ?_options(?_sort_real_scan_for_NaN, ?_dummy)
Examines each input array entry to find the first value such that
isNaN(x(i)) == .true.
See the isNaN() function, Chapter 10.
Default: Does not scan for NaNs.
FORTRAN 90 Interface
Generic: CALL SORT_REAL (X, Y [])
Specific: The specific interface names are S_SORT_REAL and D_SORT_REAL.
Description
For a detailed description, see the “Description” section of routine SVRGN, which appears later in this chapter.
Fatal and Terminal Error Messages
See the messages.gls file for error messages for SORT_REAL. These error messages are numbered 561567; 581587.
Examples
Example 1: Sorting an Array
An array of random numbers is obtained. The values are sorted so they are nondecreasing.

use sort_real_int
use rand_gen_int
implicit none
! This is Example 1 for SORT_REAL.
integer, parameter :: n=100
real(kind(1e0)), dimension(n) :: x, y

! Generate random data to sort.
call rand_gen(x)
! Sort the data so it is non-decreasing.
call sort_real(x, y)
! Check that the sorted array is not decreasing.
if (count(y(1:n-1) > y(2:n)) == 0) then
write (*,*) 'Example 1 for SORT_REAL is correct.'
end if
end
Output

Example 1 for SORT_REAL is correct.
Example 2: Sort and Final Move with a Permutation
A set of n random numbers is sorted so the results are nonincreasing. The columns of an n × n random matrix are moved to the order given by the permutation defined by the interchange of the entries. Since the routine sorts the results to be algebraically nondecreasing, the array of negative values is used as input. Thus, the negative value of the sorted output order is nonincreasing. The optional argument “iperm=” records the final order and is used to move the matrix columns to that order. This example illustrates the principle of sorting record keys, followed by direct movement of the records to sorted order.
use sort_real_int
use rand_gen_int
implicit none
! This is Example 2 for SORT_REAL.
integer i
integer, parameter :: n=100
integer ip(n)
real(kind(1e0)) a(n,n), x(n), y(n), temp(n*n)
! Generate a random array and matrix of values.
call rand_gen(x)
call rand_gen(temp)
a = reshape(temp,(/n,n/))
! Initialize permutation to the identity.
do i=1, n
ip(i) = i
end do
! Sort using negative values so the final order is
! non-increasing.
call sort_real(-x, y, iperm=ip)
! Final movement of keys and matrix columns.
y = x(ip(1:n))
a = a(:,ip(1:n))

! Check the results.
if (count(y(1:n-1) < y(2:n)) == 0) then
write (*,*) 'Example 2 for SORT_REAL is correct.'
end if
end
Output

Example 2 for SORT_REAL is correct.