| 1 |
C Copyright(C) 1999-2020 National Technology & Engineering Solutions |
| 2 |
C of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
C NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
C |
| 5 |
C See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
c$$$ program test |
| 8 |
c$$$ parameter (n=20) |
| 9 |
c$$$ dimension a(n) |
| 10 |
c$$$ integer indx(n) |
| 11 |
c$$$ |
| 12 |
c$$$ integer*4 seed |
| 13 |
c$$$ seed=0 |
| 14 |
c$$$ |
| 15 |
c$$$ call srand(time()) |
| 16 |
c$$$ do i=1, n |
| 17 |
c$$$ a(i) = rand(seed) |
| 18 |
c$$$ end do |
| 19 |
c$$$ |
| 20 |
c$$$ do i=1,n |
| 21 |
c$$$ write (*,*) a(i) |
| 22 |
c$$$ end do |
| 23 |
c$$$ write (*,*) '-------' |
| 24 |
c$$$ |
| 25 |
c$$$ call indexx(a, indx, n, .true.) |
| 26 |
c$$$ |
| 27 |
c$$$ do i=1,n |
| 28 |
c$$$ write (*,*) i, indx(i), a(indx(i)) |
| 29 |
c$$$ end do |
| 30 |
c$$$ |
| 31 |
c$$$ end |
| 32 |
|
| 33 |
C------------------------------------------------------------------------ |
| 34 |
C SUBROUTINE INDEXX: Indexes an array ARRAY, that is |
| 35 |
C it outputs an array INDX such that ARRAY(INDX(J)) is in |
| 36 |
C ascending order for J=1,2,...,N. The input quantities N and |
| 37 |
C ARRAY are not changed. |
| 38 |
|
| 39 |
C ARRAY (*) - Array to be sorted |
| 40 |
C INDX (modified) - Sorted order of ARRAY |
| 41 |
C N - Number of elements in ARRAY |
| 42 |
C INIT - .FALSE. if INDX already setup |
| 43 |
C .TRUE. if INDX must be initialized |
| 44 |
C------------------------------------------------------------------------ |
| 45 |
|
| 46 |
subroutine indexx(a, indx, n, init) |
| 47 |
|
| 48 |
dimension a(*) |
| 49 |
integer indx(0:*) |
| 50 |
integer n |
| 51 |
logical init |
| 52 |
|
| 53 |
integer start, bottom |
| 54 |
integer temp |
| 55 |
|
| 56 |
if (init) then |
| 57 |
do i=1, n |
| 58 |
indx(i-1) = i |
| 59 |
end do |
| 60 |
end if |
| 61 |
|
| 62 |
do start = (n-2)/2, 0, -1 |
| 63 |
call my_siftdown(a, indx, start, n) |
| 64 |
end do |
| 65 |
|
| 66 |
do bottom = n-1, 1, -1 |
| 67 |
temp = indx(0) |
| 68 |
indx(0) = indx(bottom) |
| 69 |
indx(bottom) = temp |
| 70 |
call my_siftdown(a, indx, 0, bottom) |
| 71 |
end do |
| 72 |
end |
| 73 |
|
| 74 |
subroutine my_siftdown(a, indx, start, bottom) |
| 75 |
|
| 76 |
real a(*) |
| 77 |
integer indx(0:*) |
| 78 |
|
| 79 |
integer start, bottom |
| 80 |
integer child, root |
| 81 |
integer temp |
| 82 |
|
| 83 |
root = start |
| 84 |
do while(root*2 + 1 .lt. bottom) |
| 85 |
child = root * 2 + 1 |
| 86 |
|
| 87 |
if ((child + 1 .lt. bottom) .and. |
| 88 |
* (a(indx(child)) .lt. a(indx(child+1)))) then |
| 89 |
child = child + 1 |
| 90 |
end if |
| 91 |
|
| 92 |
if (a(indx(root)) .lt. a(indx(child))) then |
| 93 |
temp = indx(child) |
| 94 |
indx(child) = indx(root) |
| 95 |
indx(root) = temp |
| 96 |
root = child |
| 97 |
else |
| 98 |
return |
| 99 |
end if |
| 100 |
end do |
| 101 |
return |
| 102 |
end |