Debian Codegraph @

packages/seacas/libraries/suplib/heap_sort.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 subroutine heapsort(a,n)
8
9 dimension a(0:*)
10 integer n
11
12 integer start, bottom
13 real temp
14
15 do start = (n-2)/2, 0, -1
16 call hs_siftdown(a, start, n)
17 end do
18
19 do bottom = n-1, 1, -1
20 temp = a(0)
21 a(0) = a(bottom)
22 a(bottom) = temp
23 call hs_siftdown(a, 0, bottom)
24 end do
25 end
26
27 subroutine hs_siftdown(a, start, bottom)
28
29 real a(0:*)
30 integer start, bottom
31 integer child, root
32 real temp
33
34 root = start
35 do while(root*2 + 1 .lt. bottom)
36 child = root * 2 + 1
37
38 if ((child + 1 .lt. bottom) .and.
39 * (a(child) .lt. a(child+1))) then
40 child = child + 1
41 end if
42
43 if (a(root) .lt. a(child)) then
44 temp = a(child)
45 a(child) = a (root)
46 a(root) = temp
47 root = child
48 else
49 return
50 end if
51 end do
52 return
53 end