Module_Sort_Procedures

Copyright 2024 IRD

This file is part of statpack.

statpack is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

statpack is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You can find a copy of the GNU Lesser General Public License in the statpack/doc directory.

Authors: Pascal Terray (LOCEAN/IPSL, Paris, France)


MODULE EXPORTING SORTING UTILITIES.

LATEST REVISION : 12/03/2024


subroutine tri_insert ( list )

Purpose

Sort the integer array LIST into ascending numerical order, by straight insertion. LIST is replaced on output by its sorted rearrangement.

Arguments

LIST (INPUT/OUTPUT) integer(i4b), dimension(:)
The integer vector to sort.

subroutine tri_insert ( list, order )

Purpose

Sort the integer array LIST into ascending numerical order, by straight insertion. LIST is replaced on output by its sorted rearrangement.

ORDER is an associated integer array which gives the positions of the elements in the original order.

Arguments

LIST (INPUT/OUTPUT) integer(i4b), dimension(:)
The integer vector to sort.
ORDER (OUTPUT) integer(i4b), dimension(:)
Array which gives the positions of the elements in the original order.

Further Details

The size of LIST and ORDER must match.

subroutine tri_insert ( list )

Purpose

Sort the real array LIST into ascending numerical order, by straight insertion. LIST is replaced on output by its sorted rearrangement.

Arguments

LIST (INPUT/OUTPUT) real(stnd), dimension(:)
The real vector to sort.

subroutine tri_insert ( list, order )

Purpose

Sort the real array LIST into ascending numerical order, by straight insertion. LIST is replaced on output by its sorted rearrangement.

ORDER is an associated integer array which gives the positions of the elements in the original order.

Arguments

LIST (INPUT/OUTPUT) real(stnd), dimension(:)
The real vector to sort.
ORDER (OUTPUT) integer(i4b), dimension(:)
Array which gives the positions of the elements in the original order.

Further Details

The size of LIST and ORDER must match.

subroutine quick_sort ( list, ascending )

Purpose

Sort an integer array LIST into ascending or descending order using the QuickSort algorithm. LIST is replaced on output by its sorted rearrangement.

Arguments

LIST (INPUT/OUTPUT) integer(i4b), dimension(:)
The integer vector to sort.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Sort the array list into ascending order if ASCENDING = true, or in descending order if ASCENDING = false. The default is true.

Further Details

Quick sort routine adapted (and modified to reverse order) from:

  1. Brainerd, W.S., Goldberg, C.H., and Adams, J.C., 1990:
    Programmer’s Guide to Fortran 90. McGraw-Hill, ISBN 0-07-000248-7, pages 149-150.

subroutine quick_sort ( list, ascending )

Purpose

Sort a real array LIST into ascending or descending order using the QuickSort algorithm. LIST is replaced on output by its sorted rearrangement.

Arguments

LIST (INPUT/OUTPUT) real(stnd), dimension(:)
The real vector to sort.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Sort the array list into ascending order if ASCENDING = true, or in descending order if ASCENDING = false. The default is true.

Further Details

Quick sort routine adapted (and modified to reverse order) from:

  1. Brainerd, W.S., Goldberg, C.H., and Adams, J.C., 1990:
    Programmer’s Guide to Fortran 90. McGraw-Hill, ISBN 0-07-000248-7, pages 149-150.

subroutine quick_sort ( list, order, ascending )

Purpose

Sort an integer array LIST into ascending or descending order using the QuickSort algorithm. LIST is replaced on output by its sorted rearrangement.

ORDER is an associated integer array which gives the positions of the elements in the original order.

Arguments

LIST (INPUT/OUTPUT) integer(i4b), dimension(:)
The integer vector to sort.
ORDER (OUTPUT) integer(i4b), dimension(:)
Array which gives the positions of the elements in the original order.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Sort the array list into ascending order if ASCENDING = true, or in descending order if ASCENDING = false. The default is true.

Further Details

Quick sort routine adapted from reference (1), modified to include an associated integer array, which gives the positions of the elements in the original order, and also modified to reverse order.

The sizes of LIST and ORDER must match.

For further details, see:

  1. Brainerd, W.S., Goldberg, C.H., and Adams, J.C., 1990:
    Programmer’s Guide to Fortran 90. McGraw-Hill, ISBN 0-07-000248-7, pages 149-150.

subroutine quick_sort ( list, order, ascending )

Purpose

Sort a real array LIST into ascending or descending order using the QuickSort algorithm. LIST is replaced on output by its sorted rearrangement.

ORDER is an associated integer array which gives the positions of the elements in the original order.

Arguments

LIST (INPUT/OUTPUT) real(stnd), dimension(:)
The real vector to sort.
ORDER (OUTPUT) integer(i4b), dimension(:)
Array which gives the positions of the elements in the original order.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Sort the array list into ascending order if ASCENDING = true, or in descending order if ASCENDING = false. The default is true.

Further Details

Quick sort routine adapted from reference (1), modified to include an associated integer array, which gives the positions of the elements in the original order, and also modified to reverse order.

The sizes of LIST and ORDER must match.

For further details, see:

  1. Brainerd, W.S., Goldberg, C.H., and Adams, J.C., 1990:
    Programmer’s Guide to Fortran 90. McGraw-Hill, ISBN 0-07-000248-7, pages 149-150.

subroutine do_index ( list, index )

Purpose

This subroutine indexes an integer array LIST, i.e., outputs the array INDEX of length N such that LIST(INDEX(j)) is in ascending order for j=1, 2, …, N. The input quantity LIST is not changed.

Arguments

LIST (INPUT) integer(i4b), dimension(:)
The integer vector to index.
INDEX (OUTPUT) integer(i4b), dimension(:)
The index array.

Further Details

The sizes of LIST and INDEX must match.

subroutine do_index ( list, index )

Purpose

This subroutine indexes a real array LIST, i.e., outputs the array INDEX of length N such that LIST(INDEX(j)) is in ascending order for j=1, 2, …, N. The input quantity LIST is not changed.

Arguments

LIST (INPUT) real(stnd), dimension(:)
The real vector to index.
INDEX (OUTPUT) integer(i4b), dimension(:)
The index array.

Further Details

The sizes of LIST and INDEX must match.

function rank ( index )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine returns a same-size array RANK, the corresponding table of ranks.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index.

Further Details

This function is adapted from Numerical Recipes.

subroutine reorder ( index, slave, ascending )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine makes the corresponding rearrangement of the same-size integer array SLAVE. The rearrangement is performed by means of the integer array INDEX.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index vector.
SLAVE (INPUT/OUTPUT) integer(i4b), dimension(:)
Integer vector to rearrange according to INDEX.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Rearrange SLAVE according to ascending order if ASCENDING = true, or to descending order if ASCENDING = false. The default is true.

Further Details

The size of SLAVE and INDEX must match.

subroutine reorder ( index, slave, ascending )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine makes the corresponding rearrangement of the same-size real array SLAVE. The rearrangement is performed by means of the integer array INDEX.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index vector.
SLAVE (INPUT/OUTPUT) real(stnd), dimension(:)
Real vector to rearrange according to INDEX.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Rearrange SLAVE according to ascending order if ASCENDING = true, or to descending order if ASCENDING = false. The default is true.

Further Details

The sizes of SLAVE and INDEX must match.

subroutine reorder ( index, slave, ascending )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine makes the corresponding rearrangement of the same-size complex array SLAVE. The rearrangement is performed by means of the integer array INDEX.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index vector.
SLAVE (INPUT/OUTPUT) complex(stnd), dimension(:)
Complex vector to rearrange according to INDEX.
ASCENDING (INPUT, OPTIONAL) logical(lgl)
Rearrange SLAVE according to ascending order if ASCENDING = true, or to descending order if ASCENDING = false. The default is true.

Further Details

The sizes of SLAVE and INDEX must match.

subroutine reorder ( index, slave, ascending )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine makes the corresponding rearrangement of the columns of the integer matrix SLAVE. The rearrangement is performed by means of the integer array INDEX.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index vector.
SLAVE (INPUT/OUTPUT) integer(i4b), dimension(:,:)
Integer matrix to rearrange according to INDEX.
ASCENDING (INPUT, OPTIONAL) logical(lgl)

Rearrange the columns of SLAVE according to ascending order if ASCENDING = true, or to descending order if ASCENDING = false.

The default is true.

Further Details

The size of INDEX must match the number of columns of SLAVE. The rearrangement is done in place.

subroutine reorder ( index, slave, ascending )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine makes the corresponding rearrangement of the columns of the real matrix SLAVE. The rearrangement is performed by means of the integer array INDEX.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index vector.
SLAVE (INPUT/OUTPUT) real(stnd), dimension(:,:)
Real matrix to rearrange according to INDEX.
ASCENDING (INPUT, OPTIONAL) logical(lgl)

Rearrange the columns of SLAVE according to ascending order if ASCENDING = true, or to descending order if ASCENDING = false.

The default is true.

Further Details

The size of INDEX must match the number of columns of SLAVE. The rearrangement is done in place.

subroutine reorder ( index, slave, ascending )

Purpose

Given INDEX as output from the routine DO_INDEX, this routine makes the corresponding rearrangement of the columns of the complex matrix SLAVE. The rearrangement is performed by means of the integer array INDEX.

Arguments

INDEX (INPUT) integer(i4b), dimension(:)
The index vector.
SLAVE (INPUT/OUTPUT) complex(stnd), dimension(:,:)
Complex matrix to rearrange according to INDEX.
ASCENDING (INPUT, OPTIONAL) logical(lgl)

Rearrange the columns of SLAVE according to ascending order if ASCENDING = true, or to descending order if ASCENDING = false.

The default is true.

Further Details

The size of INDEX must match the number of columns of SLAVE. The rearrangement is done in place.
Flag Counter