~hmatuschek/+junk/sdr-wspr-package

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
!-------------------------------------------------------------------------------
!
! This file is part of the WSPR application, Weak Signal Propogation Reporter
!
! File Name:    ssort.f90
! Description:  Sort an array and optionally make the same interchanges in
!               an auxiliary array.
!
! Copyright (C) 2001-2014 Joseph Taylor, K1JT
! License: GPL-3+
!
! This program is free software; you can redistribute it and/or modify it under
! the terms of the GNU General Public License as published by the Free Software
! Foundation; either version 3 of the License, or (at your option) any later
! version.
!
! This program is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
! FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
! details.
!
! You should have received a copy of the GNU General Public License along with
! this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
! Street, Fifth Floor, Boston, MA 02110-1301, USA.
!
!-------------------------------------------------------------------------------
subroutine ssort (x,y,n,kflag)
! Purpose:  Sort an array and optionally make the same interchanges in
!           an auxiliary array.  The array may be sorted in increasing
!           or decreasing order.  A slightly modified quicksort
!           algorithm is used.

!     x - array of values to be sorted
!     y - array to be (optionally) carried along
!     n - number of values in array x to be sorted
!     kflag - control parameter
!           =  2  means sort x in increasing order and carry y along.
!           =  1  means sort x in increasing order (ignoring y)
!           = -1  means sort x in decreasing order (ignoring y)
!           = -2  means sort x in decreasing order and carry y along.

  integer kflag, n
  real x(n), y(n)
  real r, t, tt, tty, ty
  integer i, ij, j, k, kk, l, m, nn
  integer il(21), iu(21)

  nn = n
  if (nn .lt. 1) then
     print*,'ssort: The number of sort elements is not positive.'
     print*,'ssort: n = ',nn,'   kflag = ',kflag
     return
  endif

  kk = abs(kflag)
  if (kk.ne.1 .and. kk.ne.2) then
     print *,'The sort control parameter, k, is not 2, 1, -1, or -2.'
     return
  endif

! Alter array x to get decreasing order if needed

  if (kflag .le. -1) then
     do i=1,nn
        x(i) = -x(i)
     enddo
  endif

  if (kk .eq. 2) go to 100

! Sort x only

  m = 1
  i = 1
  j = nn
  r = 0.375e0

20 if (i .eq. j) go to 60
  if (r .le. 0.5898437e0) then
     r = r+3.90625e-2
  else
     r = r-0.21875e0
  endif

30 k = i

! Select a central element of the array and save it in location t

  ij = i + int((j-i)*r)
  t = x(ij)

! If first element of array is greater than t, interchange with t

  if (x(i) .gt. t) then
     x(ij) = x(i)
     x(i) = t
     t = x(ij)
  endif
  l = j

! If last element of array is less than than t, interchange with t

  if (x(j) .lt. t) then
     x(ij) = x(j)
     x(j) = t
     t = x(ij)

! If first element of array is greater than t, interchange with t

     if (x(i) .gt. t) then
        x(ij) = x(i)
        x(i) = t
        t = x(ij)
     endif
  endif

! Find an element in the second half of the array which is smaller than t

40 l = l-1
  if (x(l) .gt. t) go to 40

! Find an element in the first half of the array which is greater than t

50 k = k+1
  if (x(k) .lt. t) go to 50

! Interchange these elements

  if (k .le. l) then
     tt = x(l)
     x(l) = x(k)
     x(k) = tt
     go to 40
  endif

! Save upper and lower subscripts of the array yet to be sorted

  if (l-i .gt. j-k) then
     il(m) = i
     iu(m) = l
     i = k
     m = m+1
  else
     il(m) = k
     iu(m) = j
     j = l
     m = m+1
  endif
  go to 70

! Begin again on another portion of the unsorted array

60 m = m-1
  if (m .eq. 0) go to 190
  i = il(m)
  j = iu(m)

70 if (j-i .ge. 1) go to 30
  if (i .eq. 1) go to 20
  i = i-1

80 i = i+1
  if (i .eq. j) go to 60
  t = x(i+1)
  if (x(i) .le. t) go to 80
  k = i

90 x(k+1) = x(k)
  k = k-1
  if (t .lt. x(k)) go to 90
  x(k+1) = t
  go to 80

! Sort x and carry y along

100 m = 1
  i = 1
  j = nn
  r = 0.375e0

110 if (i .eq. j) go to 150
  if (r .le. 0.5898437e0) then
     r = r+3.90625e-2
  else
     r = r-0.21875e0
  endif

120 k = i

! Select a central element of the array and save it in location t

  ij = i + int((j-i)*r)
  t = x(ij)
  ty = y(ij)

! If first element of array is greater than t, interchange with t

  if (x(i) .gt. t) then
     x(ij) = x(i)
     x(i) = t
     t = x(ij)
     y(ij) = y(i)
     y(i) = ty
     ty = y(ij)
  endif
  l = j

! If last element of array is less than t, interchange with t

  if (x(j) .lt. t) then
     x(ij) = x(j)
     x(j) = t
     t = x(ij)
     y(ij) = y(j)
     y(j) = ty
     ty = y(ij)

! If first element of array is greater than t, interchange with t

     if (x(i) .gt. t) then
        x(ij) = x(i)
        x(i) = t
        t = x(ij)
        y(ij) = y(i)
        y(i) = ty
        ty = y(ij)
     endif
  endif

! Find an element in the second half of the array which is smaller than t

130 l = l-1
  if (x(l) .gt. t) go to 130

! Find an element in the first half of the array which is greater than t

140 k = k+1
  if (x(k) .lt. t) go to 140

! Interchange these elements

  if (k .le. l) then
     tt = x(l)
     x(l) = x(k)
     x(k) = tt
     tty = y(l)
     y(l) = y(k)
     y(k) = tty
     go to 130
  endif

! Save upper and lower subscripts of the array yet to be sorted

  if (l-i .gt. j-k) then
     il(m) = i
     iu(m) = l
     i = k
     m = m+1
  else
     il(m) = k
     iu(m) = j
     j = l
     m = m+1
  endif
  go to 160

! Begin again on another portion of the unsorted array

150 m = m-1
  if (m .eq. 0) go to 190
  i = il(m)
  j = iu(m)

160 if (j-i .ge. 1) go to 120
  if (i .eq. 1) go to 110
  i = i-1

170 i = i+1
  if (i .eq. j) go to 150
  t = x(i+1)
  ty = y(i+1)
  if (x(i) .le. t) go to 170
  k = i

180 x(k+1) = x(k)
  y(k+1) = y(k)
  k = k-1
  if (t .lt. x(k)) go to 180
  x(k+1) = t
  y(k+1) = ty
  go to 170

! Clean up

190 if (kflag .le. -1) then
     do i=1,nn
        x(i) = -x(i)
     enddo
  endif

  return
end subroutine ssort