subroutine test_indnth
use M_orderpack__indnth, only : indnth
implicit none
integer,allocatable :: iarr(:)
integer :: i
integer :: imiddle
call unit_check_start('indnth', '-library orderpack') ! start tests
! find Nth lowest value in an array without sorting entire array
iarr=[80,70,30,40,50,60,20,10]
! can return the same values as intrinsics minloc() and maxloc()
call unit_check('indnth',all(indnth(iarr,1 ).eq.minloc(iarr)),'like minloc()')
call unit_check('indnth',all(indnth(iarr,size(iarr)).eq.maxloc(iarr)),'like maxloc()')
! but more generally it can return the Nth lowest value.
call unit_check('indnth',iarr(indnth(iarr,8)).eq.80,'Nth value')
! so a value at the middle would be
imiddle=(size(iarr)+1)/2
call unit_check('indnth',iarr(indnth(iarr,imiddle)).eq.40,'find median')
! sort the hard way, one value at a time
call unit_check('indnth', all([(iarr(indnth(iarr,i)),i=1,size(iarr))].eq.[10,20,30,40,50,60,70,80]),'sort hard way')
call unit_check_done('indnth',msg='test completed')
end subroutine test_indnth