35 module procedure sort_exec_without_idx
36 module procedure sort_exec_with_idxs
37 module procedure sort_exec_with_idx
38 end interface sort_exec
55 private :: partition_arg
56 private :: partition_desc
57 private :: partition_desc_arg
58 private :: median_of_three
59 private :: median_of_three_arg
60 private :: sample_second_min
61 private :: sample_second_min_arg
62 private :: sample_second_max
63 private :: sample_second_max_arg
70 subroutine sort_exec_with_idxs( &
77 integer,
intent(in) :: npoints
78 real(RP),
intent(inout) :: val (npoints)
79 integer,
intent(inout) :: idx_i(npoints)
80 integer,
intent(inout) :: idx_j(npoints)
82 logical,
intent(in),
optional :: reverse
93 if (
present(reverse) )
then
94 if ( reverse ) sig = -1.0_rp
99 if ( val(n1) * sig > val(n2) * sig )
then
104 idx_i(n1) = idx_i(n2)
105 idx_j(n1) = idx_j(n2)
116 end subroutine sort_exec_with_idxs
119 subroutine sort_exec_with_idx( &
125 integer,
intent(in) :: npoints
126 real(RP),
intent(inout) :: val (npoints)
127 integer,
intent(inout) :: index(npoints)
129 logical,
intent(in),
optional :: reverse
139 if (
present(reverse) )
then
140 if ( reverse ) sig = -1.0_rp
144 do n2 = n1+1, npoints
145 if ( val(n1) * sig > val(n2) * sig )
then
149 index(n1) = index(n2)
159 end subroutine sort_exec_with_idx
162 subroutine sort_exec_without_idx( &
168 integer,
intent(in) :: npoints
169 real(RP),
intent(inout) :: val (npoints)
171 logical,
intent(in),
optional :: reverse
180 if (
present(reverse) )
then
181 if ( reverse ) sig = -1.0_rp
185 do n2 = n1+1, npoints
186 if ( val(n1) * sig > val(n2) * sig )
then
197 end subroutine sort_exec_without_idx
200 integer(DP),
intent(in) :: n
201 integer(DP),
intent(inout) :: ary(n)
202 integer(DP),
intent(out) :: c
208 if(j .ne. ary(i))
then
220 integer(DP),
intent(in) :: n
221 integer(DP),
intent(inout) :: array(n)
222 integer(DP) :: i, j, k, k1, kn2, kn, tmp
223 integer,
parameter :: threshold = 4
256 if(array(i) .ge. k)
exit
259 if(array(j) .le. k)
exit
269 if(i - 1 > threshold)
then
271 else if(i - 1 > 1)
then
274 if(n - j > threshold)
then
276 else if(n - j > 1)
then
285 integer(DP),
intent(in) :: n
286 integer(DP),
intent(inout) :: array(n)
288 integer(DP) :: i, j, k, l
310 if(array(j) .lt. array(j + 1)) j = j + 1
312 if(t .lt. array(j))
then
328 real(
rp),
intent(inout) :: a(:)
329 integer,
intent(in) :: left, right
330 integer,
intent(in) :: k
331 integer :: middle, pivot
333 if (left < right)
then
334 if ((right-left)/k >= 2)
then
335 call sample_second_min(a, left, right, k, pivot)
337 middle = (left + right) / 2
338 call median_of_three(a, left, middle, right, pivot)
340 call partition(a, left, right, pivot)
343 else if (k > pivot)
then
353 real(
rp),
intent(in) :: a(:)
354 integer,
intent(inout) :: x(:)
355 integer,
intent(in) :: left, right
356 integer,
intent(in) :: k
357 integer :: middle, pivot
359 if (left < right)
then
360 if ((right-left)/k >= 2)
then
361 call sample_second_min_arg(a, x, left, right, k, pivot)
363 middle = (left + right) / 2
364 call median_of_three_arg(a, x, left, middle, right, pivot)
366 call partition_arg(a, x, left, right, pivot)
369 else if (k > pivot)
then
379 real(
rp),
intent(inout) :: a(:)
380 integer,
intent(in) :: left, right
381 integer,
intent(in) :: k
382 integer :: middle, pivot
384 if (left < right)
then
385 if ((right-left)/k >= 2)
then
386 call sample_second_max(a, left, right, k, pivot)
388 middle = (left + right) / 2
389 call median_of_three(a, left, middle, right, pivot)
391 call partition_desc(a, left, right, pivot)
394 else if (k > pivot)
then
404 real(
rp),
intent(in) :: a(:)
405 integer,
intent(inout) :: x(:)
406 integer,
intent(in) :: left, right
407 integer,
intent(in) :: k
408 integer :: middle, pivot
410 if (left < right)
then
411 if ((right-left)/k >= 2)
then
412 call sample_second_max_arg(a, x, left, right, k, pivot)
414 middle = (left + right) / 2
415 call median_of_three_arg(a, x, left, middle, right, pivot)
417 call partition_desc_arg(a, x, left, right, pivot)
420 else if (k > pivot)
then
430 subroutine swap(x, y)
432 real(
rp),
intent(inout) :: x, y
441 subroutine swap_i(x, y)
443 integer,
intent(inout) :: x, y
450 end subroutine swap_i
452 subroutine partition(A, left, right, pivot)
454 real(
rp),
intent(inout) :: a(:)
455 integer,
intent(in) :: left, right
456 integer,
intent(inout) :: pivot
458 integer :: idx, store_idx
461 call swap(a(pivot), a(right))
464 do idx = left, right-1
465 if (a(idx) < a_pivot)
then
466 call swap(a(store_idx), a(idx))
467 store_idx = store_idx + 1
471 call swap(a(right), a(store_idx))
475 end subroutine partition
477 subroutine partition_arg(A, X, left, right, pivot)
479 real(
rp),
intent(in) :: a(:)
480 integer,
intent(inout) :: x(:)
481 integer,
intent(in) :: left, right
482 integer,
intent(inout) :: pivot
484 integer :: idx, store_idx
486 a_pivot = a(x(pivot))
487 call swap_i(x(pivot), x(right))
490 do idx = left, right-1
491 if (a(x(idx)) < a_pivot)
then
492 call swap_i(x(store_idx), x(idx))
493 store_idx = store_idx + 1
497 call swap_i(x(right), x(store_idx))
501 end subroutine partition_arg
503 subroutine partition_desc(A, left, right, pivot)
505 real(
rp),
intent(inout) :: a(:)
506 integer,
intent(in) :: left, right
507 integer,
intent(inout) :: pivot
509 integer :: idx, store_idx
512 call swap(a(pivot), a(right))
515 do idx = left, right-1
516 if (a(idx) > a_pivot)
then
517 call swap(a(store_idx), a(idx))
518 store_idx = store_idx + 1
522 call swap(a(right), a(store_idx))
526 end subroutine partition_desc
528 subroutine partition_desc_arg(A, X, left, right, pivot)
530 real(
rp),
intent(in) :: a(:)
531 integer,
intent(inout) :: x(:)
532 integer,
intent(in) :: left, right
533 integer,
intent(inout) :: pivot
535 integer :: idx, store_idx
537 a_pivot = a(x(pivot))
538 call swap_i(x(pivot), x(right))
541 do idx = left, right-1
542 if (a(x(idx)) > a_pivot)
then
543 call swap_i(x(store_idx), x(idx))
544 store_idx = store_idx + 1
548 call swap_i(x(right), x(store_idx))
552 end subroutine partition_desc_arg
554 subroutine median_of_three(A, i1, i2, i3, i)
556 real(
rp),
intent(in) :: a(:)
557 integer,
intent(in) :: i1, i2, i3
558 integer,
intent(out) :: i
560 if (a(i1) < a(i2))
then
561 if (a(i2) < a(i3))
then
563 else if (a(i1) < a(i3))
then
569 if (a(i1) < a(i3))
then
571 else if (a(i2) < a(i3))
then
579 end subroutine median_of_three
581 subroutine median_of_three_arg(A, X, i1, i2, i3, i)
583 real(
rp),
intent(in) :: a(:)
584 integer,
intent(in) :: x(:)
585 integer,
intent(in) :: i1, i2, i3
586 integer,
intent(out) :: i
588 if (a(x(i1)) < a(x(i2)))
then
589 if (a(x(i2)) < a(x(i3)))
then
591 else if (a(x(i1)) < a(x(i3)))
then
597 if (a(x(i1)) < a(x(i3)))
then
599 else if (a(x(i2)) < a(x(i3)))
then
607 end subroutine median_of_three_arg
609 subroutine sample_second_min(A, left, right, K, i_2)
611 real(
rp),
intent(in) :: a(:)
612 integer,
intent(in) :: left, right
613 integer,
intent(in) :: k
614 integer,
intent(out) :: i_2
615 real(
rp) :: a_min, a_min_2
622 do j = left, right, k
623 if (a(j) < a_min)
then
628 else if (a(j) < a_min_2)
then
635 end subroutine sample_second_min
637 subroutine sample_second_min_arg(A, X, left, right, K, i_2)
639 real(
rp),
intent(in) :: a(:)
640 integer,
intent(in) :: x(:)
641 integer,
intent(in) :: left, right
642 integer,
intent(in) :: k
643 integer,
intent(out) :: i_2
644 real(
rp) :: a_min, a_min_2
651 do j = left, right, k
652 if (a(x(j)) < a_min)
then
657 else if (a(x(j)) < a_min_2)
then
664 end subroutine sample_second_min_arg
666 subroutine sample_second_max(A, left, right, K, i_2)
668 real(
rp),
intent(in) :: a(:)
669 integer,
intent(in) :: left, right
670 integer,
intent(in) :: k
671 integer,
intent(out) :: i_2
672 real(
rp) :: a_max, a_max_2
677 a_max = 0.0d0 - huge(a)
678 a_max_2 = 0.0d0 - huge(a)
679 do j = left, right, k
680 if (a(j) > a_max)
then
685 else if (a(j) > a_max_2)
then
692 end subroutine sample_second_max
694 subroutine sample_second_max_arg(A, X, left, right, K, i_2)
696 real(
rp),
intent(in) :: a(:)
697 integer,
intent(in) :: x(:)
698 integer,
intent(in) :: left, right
699 integer,
intent(in) :: k
700 integer,
intent(out) :: i_2
701 real(
rp) :: a_max, a_max_2
706 a_max = 0.0d0 - huge(a)
707 a_max_2 = 0.0d0 - huge(a)
708 do j = left, right, k
709 if (a(x(j)) > a_max)
then
714 else if (a(x(j)) > a_max_2)
then
721 end subroutine sample_second_max_arg