The generic subroutine implementing the ORD_SORT
algorithm to return
an input array with its elements sorted in order of (non-)decreasing
value. Its use has the syntax:
call ord_sort( array[, work, reverse] )
with the arguments:
array: the rank 1 array to be sorted. It is an intent(inout)
argument of any of the types integer(int8)
, integer(int16)
,
integer(int32)
, integer(int64)
, real(real32)
, real(real64)
,
real(real128)
, character(*)
, type(string_type)
,
type(bitset_64)
, type(bitset_large)
. If both the
type of array
is real and at least one of the elements is a
NaN
, then the ordering of the result is undefined. Otherwise it
is defined to be the original elements in non-decreasing order.
work (optional): shall be a rank 1 array of the same type as
array
, and shall have at least size(array)/2
elements. It is an
intent(out)
argument to be used as “scratch” memory
for internal record keeping. If associated with an array in static
storage, its use can significantly reduce the stack memory requirements
for the code. Its value on return is undefined.
reverse
(optional): shall be a scalar of type default logical. It
is an intent(in)
argument. If present with a value of .true.
then
array
will be sorted in order of non-increasing values in stable
order. Otherwise index will sort array
in order of non-decreasing
values in stable order.
...
! Read arrays from sorted files
call read_sorted_file( 'dummy_file1', array1 )
call read_sorted_file( 'dummy_file2', array2 )
! Concatenate the arrays
allocate( array( size(array1) + size(array2) ) )
array( 1:size(array1) ) = array1(:)
array( size(array1)+1:size(array1)+size(array2) ) = array2(:)
! Sort the resulting array
call ord_sort( array, work )
! Process the sorted array
call array_search( array, values )
...
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer, | private, | parameter | :: | max_merge_stack | = | int(ceiling(log(2._dp**64)/log(1.6180339887_dp))) |
The generic subroutine interface implementing the ORD_SORT
algorithm,
a translation to Fortran 2008, of the "Rust" sort
algorithm found in
slice.rs
https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs#L2159
ORD_SORT
is a hybrid stable comparison algorithm combining merge sort
,
and insertion sort
.
It is always at worst O(N Ln(N)) in sorting random
data, having a performance about 25% slower than SORT
on such
data, but has much better performance than SORT
on partially
sorted data, having O(N) performance on uniformly non-increasing or
non-decreasing data.
char_ord_sort( array )
sorts the input ARRAY
of type character(len=*)
using a hybrid sort based on the "Rust" sort
algorithm found in slice.rs
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(inout) | :: | array(0:) | |||
character(len=len), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
dp_ord_sort( array )
sorts the input ARRAY
of type real(dp)
using a hybrid sort based on the "Rust" sort
algorithm found in slice.rs
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(inout) | :: | array(0:) | |||
real(kind=dp), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
int32_ord_sort( array )
sorts the input ARRAY
of type integer(int32)
using a hybrid sort based on the "Rust" sort
algorithm found in slice.rs
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int32), | intent(inout) | :: | array(0:) | |||
integer(kind=int32), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
int64_ord_sort( array )
sorts the input ARRAY
of type integer(int64)
using a hybrid sort based on the "Rust" sort
algorithm found in slice.rs
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int64), | intent(inout) | :: | array(0:) | |||
integer(kind=int64), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
sp_ord_sort( array )
sorts the input ARRAY
of type real(sp)
using a hybrid sort based on the "Rust" sort
algorithm found in slice.rs
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=sp), | intent(inout) | :: | array(0:) | |||
real(kind=sp), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
Used to pass state around in a stack among helper functions for the
ORD_SORT
and SORT_INDEX
algorithms
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer(kind=int_index), | public | :: | base | = | 0 | ||
integer(kind=int_index), | public | :: | len | = | 0 |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(inout) | :: | array(0:) | |||
character(len=len), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(inout) | :: | array(0:) | |||
character(len=len), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(inout) | :: | array(0:) | |||
character(len=len), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(inout) | :: | array(0:) | |||
real(kind=dp), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(inout) | :: | array(0:) | |||
real(kind=dp), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(inout) | :: | array(0:) | |||
real(kind=dp), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int32), | intent(inout) | :: | array(0:) | |||
integer(kind=int32), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int32), | intent(inout) | :: | array(0:) | |||
integer(kind=int32), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int32), | intent(inout) | :: | array(0:) | |||
integer(kind=int32), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int64), | intent(inout) | :: | array(0:) | |||
integer(kind=int64), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int64), | intent(inout) | :: | array(0:) | |||
integer(kind=int64), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int64), | intent(inout) | :: | array(0:) | |||
integer(kind=int64), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=sp), | intent(inout) | :: | array(0:) | |||
real(kind=sp), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=sp), | intent(inout) | :: | array(0:) | |||
real(kind=sp), | intent(out), | optional | :: | work(0:) |
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=sp), | intent(inout) | :: | array(0:) | |||
real(kind=sp), | intent(out), | optional | :: | work(0:) | ||
logical, | intent(in), | optional | :: | reverse |