NuMFor 9f2ab49 (2024-04-08)
Numerical (Modern) Fortran. Library for Simple Numerical computing
sorting Module Reference

sort provides a framework for searching elements and sorting arrays More...

Data Types

interface  searchsorted
 searchsorted: Find index where an element should be inserted in an array to maintain order. More...
 

Functions/Subroutines

subroutine, public sort (vec, reverse)
 Routine to sort a vector.
 

Variables

integer, parameter minsize_bisection = 100
 Minimum value of elements for using bisection.
 

Detailed Description

sort provides a framework for searching elements and sorting arrays

Function/Subroutine Documentation

◆ sort()

subroutine, public sort ( real(dp), dimension(:), intent(inout) vec,
logical, intent(in), optional reverse )

Routine to sort a vector.

Sorting routine, slightly modified from the original sources:

It uses a simple insertion point algorithm for small arrays (less than 20 elements) and quicksort algorithm for larger arrays.

Examples:

real(dp), dimension(4) :: a = [ 1._dp, 0.5_dp, -2._dp, 9._dp]
call sort(a)
! gives: -2 0.5 1 9
call sort(a, reverse=.true.)
! gives: 9 1 0.5 -2
Note
Implementation detail: Sorting in descending order is currently implemented by sorting -vec and later multiplying by (-1) again. From benchmark appears that there is not an important efficiency impact. Further study would be welcome.
Parameters
[in,out]vecVector to sort
[in]reverse.TRUE. to sort in descending order