sort_indexed Interface

public interface sort_indexed

Module Procedures

private function sort_int8(input) result(counts)

NAME

sort_indexed(3f) - [M_sort] indexed sort of an array
(LICENSE:PD)

SYNOPSIS

  function sort_indexed(data) result(indx)

   TYPE,intent(in) :: data
   integer :: indx(size(data))

DESCRIPTION

This routine is very slow on large arrays but I liked writing a sort
routine with one executable line!

An indexed sort of an array. The data is not moved. An integer array is
generated instead with values that are indices to the sorted order of
the data. This requires a second array the size of the input array,
which for large arrays could require a significant amount of memory. One
major advantage of this method is that any element of a user-defined type
that is a scalar intrinsic can be used to provide the sort data and
subsequently the indices can be used to access the entire user-defined
type in sorted order. This makes this seemingly simple sort procedure
usuable with the vast majority of user-defined types.

OPTIONS

 DATA   an array of type REAL, INTEGER, or CHARACTER to be sorted

RETURNS

 INDEX  an INTEGER array of default kind that contains the sorted
        indices.

EXAMPLE

Sample usage:

program demo_sort_indexed
use M_sort, only : sort_indexed
implicit none
integer,parameter            :: isz=10000
real                         :: rr(isz)
integer                      :: i
write(*,*)'initializing array with ',isz,' random numbers'
CALL RANDOM_NUMBER(RR)
rr=rr*450000.0
! use the index array to actually move the input array into a sorted order
rr=rr(sort_indexed(rr))
! or
!rr(sort_indexed(rr))=rr
write(*,*)'checking if values are sorted(3f)'
do i=1,isz-1
   if(rr(i).gt.rr(i+1))then
      write(*,*)'Error in sorting reals small to large ',i,rr(i),rr(i+1)
   endif
enddo
write(*,*)'test of sort_indexed(3f) complete'
end program demo_sort_indexed

Results:

Arguments

Type IntentOptional Attributes Name
integer(kind=int8), intent(in) :: input(:)

Return Value integer, (size(input))

private function sort_int16(input) result(counts)

Arguments

Type IntentOptional Attributes Name
integer(kind=int16), intent(in) :: input(:)

Return Value integer, (size(input))

private function sort_int32(input) result(counts)

Arguments

Type IntentOptional Attributes Name
integer(kind=int32), intent(in) :: input(:)

Return Value integer, (size(input))

private function sort_int64(input) result(counts)

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(in) :: input(:)

Return Value integer, (size(input))

private function sort_real32(input) result(counts)

Arguments

Type IntentOptional Attributes Name
real(kind=real32), intent(in) :: input(:)

Return Value integer, (size(input))

private function sort_real64(input) result(counts)

Arguments

Type IntentOptional Attributes Name
real(kind=real64), intent(in) :: input(:)

Return Value integer, (size(input))

private function sort_character(input) result(counts)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: input(:)

Return Value integer, (size(input))