MODULE Sort_Procedures¶
Module Sort_Procedures exports routines for sorting vectors and matrices of real type of kind stnd and integer type of kind i4b. These two kind types are defined in module Select_Parameters.
Routines are provided for sorting data, both directly and indirectly (using an index) and are mostly based on the Quicksort algorithm [Knuth:1997] [Sedgewick:1998], which is a recursive algorithm. Routines are also provided to compute the ranks of the elements of a real or integer vector. The rank of an element is its order in the sorted data. The vector of ranks is the inverse permutation of the index permutation, which gives the order of the elements in their original sequence after sorting.
In order to use one of these routines, you must include an appropriate use Sort_Procedures
or use Statpack
statement
in your Fortran program, like:
use Sort_Procedures, only: tri_insert
or :
use Statpack, only: tri_insert
Here is the list of the public routines exported by module Sort_Procedures:
-
tri_insert
()¶
Purpose:
tri_insert() sort the integer or real array LIST into ascending numerical order, by straight insertion. LIST is replaced on output by its sorted rearrangement and the optional vector integer argument ORDER gives the positions of the elements in the original order.
Synopsis:
call tri_insert( list(:n) ) ! list is a real array call tri_insert( list(:n) , order(:n) ) ! list is a real array call tri_insert( list(:n) ) ! list is an integer array call tri_insert( list(:n) , order(:n) ) ! list is an integer array
-
quick_sort
()¶
Purpose:
quick_sort() sort the integer or real array LIST into ascending or descending numerical order, by the Quicksort algorithm [Knuth:1997] [Sedgewick:1998]. LIST is replaced on output by its sorted rearrangement and the optional vector integer argument ORDER gives the positions of the elements in the original order.
Synopsis:
call quick_sort( list(:n) , ascending=ascending ) ! list is a real array call quick_sort( list(:n) , order(:n) , ascending=ascending ) ! list is a real array call quick_sort( list(:n) , ascending=ascending ) ! list is an integer array call quick_sort( list(:n) , order(:n) , ascending=ascending ) ! list is an integer array
Examples:
-
do_index
()¶
purpose:
do_index() indexes an integer or real array LIST, i.e., outputs the array index INDEX
of length n
such that LIST(INDEX(j))
is in ascending order for j=1, 2, ..., n
.
The input array LIST is not changed.
Synopsis:
call do_index( list(:n) , index(:n) ) ! list is a real array call do_index( list(:n) , index(:n) ) ! list is an integer array
Examples:
-
rank
()¶
purpose:
Given an integer index array index
as output from the routine do_index()
, rank() returns
a same-size integer vector rank
, the corresponding array of ranks.
Synopsis:
vec(:n) = rank( index(:n) )
-
reorder
()¶
purpose:
Given an integer index array index
as output from the routine do_index()
, reorder() makes
the corresponding rearrangement of the same-size integer or real array slave
.
The rearrangement is performed by means of the index array index
and the logical input argument ascending.
Synopsis:
call reorder( index(:n) , slave(:n), ascending=ascending ) ! slave is a real array call reorder( index(:n) , slave(:p,:n), ascending=ascending ) ! slave is a real array call reorder( index(:n) , slave(:n), ascending=ascending ) ! slave is an integer array call reorder( index(:n) , slave(:p,:n), ascending=ascending ) ! slave is an integer array call reorder( index(:n) , slave(:n), ascending=ascending ) ! slave is a complex array call reorder( index(:n) , slave(:p,:n), ascending=ascending ) ! slave is a complex array
Examples: