Debian Codegraph @

packages/seacas/libraries/suplib/indexn.f

raw
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 INDEXN: 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 (NROW, *) - Array to be sorted, sorted on row IROW
40 C NROW - Row dimension of ARRAY
41 C IROW - Row of ARRAY to be sorted
42 C INDX (modified) - Sorted order of ARRAY
43 C N - Number of elements in ARRAY
44 C INIT - .FALSE. if INDX already setup
45 C .TRUE. if INDX must be initialized
46 C------------------------------------------------------------------------
47
48 subroutine indexn (a, nrow, irow, indx, n, init)
49
50 dimension a(nrow, *)
51 integer indx(0:*)
52 integer n
53 logical init
54
55 integer start, bottom
56 integer temp
57
58 if (init) then
59 do i=1, n
60 indx(i-1) = i
61 end do
62 end if
63
64 do start = (n-2)/2, 0, -1
65 call siftdownn(a, nrow, irow, indx, start, n)
66 end do
67
68 do bottom = n-1, 1, -1
69 temp = indx(0)
70 indx(0) = indx(bottom)
71 indx(bottom) = temp
72 call siftdownn(a, nrow, irow, indx, 0, bottom)
73 end do
74 end
75
76 subroutine siftdownn(a, nrow, irow, indx, start, bottom)
77
78 real a(nrow,*)
79 integer indx(0:*)
80
81 integer start, bottom
82 integer child, root
83 integer temp
84
85 root = start
86 do while(root*2 + 1 .lt. bottom)
87 child = root * 2 + 1
88
89 if (child + 1 .lt. bottom) then
90 if ((a(irow, indx(child)) .lt. a(irow, indx(child+1)))) then
91 child = child + 1
92 end if
93 end if
94
95 if (a(irow, indx(root)) .lt. a(irow, indx(child))) then
96 temp = indx(child)
97 indx(child) = indx(root)
98 indx(root) = temp
99 root = child
100 else
101 return
102 end if
103 end do
104 return
105 end