-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.f90
139 lines (91 loc) · 3.29 KB
/
sort.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
!------------------------------------------------------------!
! This file is distributed as part of the cpw2000 code and !
! under the terms of the GNU General Public License. See the !
! file `LICENSE' in the root directory of the cpw2000 !
! distribution, or http://www.gnu.org/copyleft/gpl.txt !
! !
! The webpage of the cpw2000 code is not yet written !
! !
! The cpw2000 code is hosted on GitHub: !
! !
! https://github.com/jlm785/cpw2000 !
!------------------------------------------------------------!
!> indexes a real array by the heapsort method
!> adapted from http://rosettacode.org
!> see also W. H. Preuss et al. Numerical Recipes
subroutine sort(n,a,indx)
! written 24 June 2013. JLM
! Modified documentation August 2019. JLM
! copyright J.L.Martins, INESC-MN.
! version 4.94
implicit none
integer, parameter :: REAL64 = selected_real_kind(12)
! input
integer, intent(in) :: n !< length of array
real(REAL64), intent(in) :: a(n) !< array to be indexed
! output
integer, intent(out) :: indx(n) !< index of array a
! local variables
integer :: iroot,ichild,istart,ibot,indxt,ic
if(n < 1) return
do ichild=1,n
indx(ichild) = ichild
enddo
if(n == 1) return
! hiring phase
ibot = n
do istart = n/2,1,-1
indxt = indx(istart)
iroot = istart
! long enough siftdown loop does not exceed ~log(n)/log(2)
do ic = 1,n+5
ichild = 2*iroot
if(ichild <= ibot) then
if(ichild < ibot) then
if(a(indx(ichild)) < a(indx(ichild+1))) &
& ichild = ichild + 1
endif
if(a(indxt) < a(indx(ichild))) then
indx(iroot) = indx(ichild)
iroot = ichild
else
exit
endif
else
exit
endif
enddo
indx(iroot) = indxt
enddo
! retirement and promotion phase
istart = 1
do ibot = n-1,1,-1
indxt = indx(ibot+1)
indx(ibot+1) = indx(1)
if(ibot == 1) then
exit
endif
iroot = istart
! long enough siftdown loop does not exceed ~log(n)/log(2) (repeated...)
do ic = 1,n+5
ichild = 2*iroot
if(ichild <= ibot) then
if(ichild < ibot) then
if(a(indx(ichild)) < a(indx(ichild+1))) &
& ichild = ichild + 1
endif
if(a(indxt) < a(indx(ichild))) then
indx(iroot) = indx(ichild)
iroot = ichild
else
exit
endif
else
exit
endif
enddo
indx(iroot) = indxt
enddo
indx(1) = indxt
return
end subroutine sort