|
| 1 | +program example_arg_select |
| 2 | + use stdlib_selection, only: arg_select |
| 3 | + implicit none |
| 4 | + |
| 5 | + real, allocatable :: array(:) |
| 6 | + integer, allocatable :: indx(:) |
| 7 | + integer :: kth_smallest |
| 8 | + integer :: k |
| 9 | + |
| 10 | + array = [3., 2., 7., 4., 5., 1., 4., -1.] |
| 11 | + indx = [(k, k=1, size(array))] |
| 12 | + |
| 13 | + k = 2 |
| 14 | + call arg_select(array, indx, k, kth_smallest) |
| 15 | + print *, array(kth_smallest) ! print 1.0 |
| 16 | + |
| 17 | + k = 7 |
| 18 | +! Due to the previous call to arg_select, we know for sure this is in an |
| 19 | +! index >= 2 |
| 20 | + call arg_select(array, indx, k, kth_smallest, left=2) |
| 21 | + print *, array(kth_smallest) ! print 5.0 |
| 22 | + |
| 23 | + k = 6 |
| 24 | +! Due to the previous two calls to arg_select, we know for sure this is in |
| 25 | +! an index >= 2 and <= 7 |
| 26 | + call arg_select(array, indx, k, kth_smallest, left=2, right=7) |
| 27 | + print *, array(kth_smallest) ! print 4.0 |
| 28 | + |
| 29 | +end program example_arg_select |
0 commit comments