ArgInsertionSort
This routine performs an indirect insertion sort on an array.
It returns the integer array which can be used for obtaining the sorted array.
Calling example:
CALL ArgInsertionSort(array, arg, low, high)
Then, array(arg)
will be sorted in increasing order.
Interface
- ܀ Interface
- ️܀ See example
- ↢
MODULE PURE SUBROUTINE ArgInsertionSort(array, arg, low, high)
INTEGER(Int8| Int16 | Int32 | Int64) | REAL(Real32| Real64), INTENT(IN) :: array(:)
INTEGER(I4B), INTENT(INOUT) :: arg(:)
INTEGER(I4B), INTENT(IN) :: low
INTEGER(I4B), INTENT(IN) :: high
END SUBROUTINE ArgInsertionSort
- Here, entries of
array
are NOT modified by the routine. arg
should be allocated by the userarray(arg)
returns the sorted array.
In this example we test ArgInsertionSort.
PROGRAM main
USE easifemBase
REAL(REAL64) :: avec(10)
INTEGER( I4B ) :: arg(10)
CALL RANDOM_NUMBER(avec)
avec = avec * 10
arg = arange(1, 10, 1)
CALL Display( avec, msg="unsorted array", advance="NO", full=.true. )
CALL ArgInsertionSort(avec, arg, 1, size(arg))
CALL Display( arg, msg="arg", advance="NO", full=.true. )
CALL Display( avec(arg), msg="sorted array", full=.true. )
CALL blanklines()
See results
results
unsorted array, arg, sorted array
--------------, ---, ------------
9.74747 , 8 , 0.73612
6.28162 , 10 , 0.96830
7.10011 , 6 , 1.11567
1.87641 , 4 , 1.87641
6.14801 , 9 , 2.89498
1.11567 , 5 , 6.14801
9.07674 , 2 , 6.28162
0.73612 , 3 , 7.10011
2.89498 , 7 , 9.07674
0.96830 , 1 , 9.74747
END PROGRAM main