CalculiX  2.13
A Free Software Three-Dimensional Structural Finite Element Program
dsort.f File Reference

Go to the source code of this file.

Functions/Subroutines

subroutine dsort (dx, iy, n, kflag)
 

Function/Subroutine Documentation

◆ dsort()

subroutine dsort ( double precision, dimension(*), intent(inout)  dx,
integer, dimension(*), intent(inout)  iy,
integer, intent(in)  n,
integer, intent(in)  kflag 
)
6 c
7 c slight change: XERMSG was removed; error messages are
8 c led to the screen;
9 c
10 C***BEGIN PROLOGUE DSORT
11 C***PURPOSE Sort an array and optionally make the same interchanges in
12 C an auxiliary array. The array may be sorted in increasing
13 C or decreasing order. A slightly modified QUICKSORT
14 C algorithm is used.
15 C***LIBRARY SLATEC
16 C***CATEGORY N6A2B
17 C***TYPE DOUBLE PRECISION (SSORT-S, DSORT-D, ISORT-I)
18 C***KEYWORDS SINGLETON QUICKSORT, SORT, SORTING
19 C***AUTHOR Jones, R. E., (SNLA)
20 C Wisniewski, J. A., (SNLA)
21 C***ROUTINES CALLED XERMSG
22 C***DESCRIPTION
23 C
24 C DSORT sorts array DX and optionally makes the same interchanges in
25 C array IY. The array DX may be sorted in increasing order or
26 C decreasing order. A slightly modified quicksort algorithm is used.
27 C
28 C Description of Parameters
29 C DX - array of values to be sorted (usually abscissas)
30 C IY - array to be (optionally) carried along
31 C N - number of values in array DX to be sorted
32 C KFLAG - control parameter
33 C = 2 means sort DX in increasing order and carry IY along.
34 C = 1 means sort DX in increasing order (ignoring IY)
35 C = -1 means sort DX in decreasing order (ignoring IY)
36 C = -2 means sort DX in decreasing order and carry IY along.
37 C
38 C***REFERENCES R. C. Singleton, Algorithm 347, An efficient algorithm
39 C for sorting with minimal storage, Communications of
40 C the ACM, 12, 3 (1969), pp. 185-187.
41 C***REVISION HISTORY (YYMMDD)
42 C 761101 DATE WRITTEN
43 C 761118 Modified to use the Singleton quicksort algorithm. (JAW)
44 C 890531 Changed all specific intrinsics to generic. (WRB)
45 C 890831 Modified array declarations. (WRB)
46 C 891009 Removed unreferenced statement labels. (WRB)
47 C 891024 Changed category. (WRB)
48 C 891024 REVISION DATE from Version 3.2
49 C 891214 Prologue converted to Version 4.0 format. (BAB)
50 C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
51 C 901012 Declared all variables; changed X,Y to DX,IY; changed
52 C code to parallel SSORT. (M. McClain)
53 C 920501 Reformatted the REFERENCES section. (DWL, WRB)
54 C 920519 Clarified error messages. (DWL)
55 C 920801 Declarations section rebuilt and code restructured to use
56 C IF-THEN-ELSE-ENDIF. (RWC, WRB)
57 ! 100411 changed the dimension of IL and IU from 21 to 31.
58 ! 150514 inserted intent statements
59 !
60 ! field IL and IU have the dimension 31. This is log2 of the largest
61 ! array size to be sorted. If arrays larger than 2**31 in length have
62 ! to be sorted, this dimension has to be modified accordingly
63 !
64 C***END PROLOGUE DSORT
65  implicit none
66 C .. Scalar Arguments ..
67  integer kflag, n,iy(*),ty,tty
68 C .. Array Arguments ..
69  double precision dx(*)
70 C .. Local Scalars ..
71  double precision r, t, tt
72  integer i, ij, j, k, kk, l, m, nn
73 C .. Local Arrays ..
74  integer il(31), iu(31)
75 C .. External Subroutines ..
76 c EXTERNAL XERMSG
77 C .. Intrinsic Functions ..
78  intrinsic abs, int
79 !
80  intent(in) n,kflag
81 !
82  intent(inout) dx,iy
83 C***FIRST EXECUTABLE STATEMENT DSORT
84  nn = n
85  if (nn .lt. 1) then
86  write(*,*) '*error in dsort: the number of values to be'
87  write(*,*) ' sorted is not positive'
88  call exit(201)
89  endif
90 C
91  kk = abs(kflag)
92  if (kk.ne.1 .and. kk.ne.2) then
93  write(*,*) '*error in dsort: the sort control parameter is'
94  write(*,*) ' not 2, 1, -1 or -2'
95  call exit(201)
96  endif
97 C
98 C Alter array DX to get decreasing order if needed
99 C
100  if (kflag .le. -1) then
101  do 10 i=1,nn
102  dx(i) = -dx(i)
103  10 continue
104  endif
105 C
106  if (kk .eq. 2) go to 100
107 C
108 C Sort DX only
109 C
110  m = 1
111  i = 1
112  j = nn
113  r = 0.375d0
114 C
115  20 if (i .eq. j) go to 60
116  if (r .le. 0.5898437d0) then
117  r = r+3.90625d-2
118  else
119  r = r-0.21875d0
120  endif
121 C
122  30 k = i
123 C
124 C Select a central element of the array and save it in location T
125 C
126  ij = i + int((j-i)*r)
127  t = dx(ij)
128 C
129 C If first element of array is greater than T, interchange with T
130 C
131  if (dx(i) .gt. t) then
132  dx(ij) = dx(i)
133  dx(i) = t
134  t = dx(ij)
135  endif
136  l = j
137 C
138 C If last element of array is less than than T, interchange with T
139 C
140  if (dx(j) .lt. t) then
141  dx(ij) = dx(j)
142  dx(j) = t
143  t = dx(ij)
144 C
145 C If first element of array is greater than T, interchange with T
146 C
147  if (dx(i) .gt. t) then
148  dx(ij) = dx(i)
149  dx(i) = t
150  t = dx(ij)
151  endif
152  endif
153 C
154 C Find an element in the second half of the array which is smaller
155 C than T
156 C
157  40 l = l-1
158  if (dx(l) .gt. t) go to 40
159 C
160 C Find an element in the first half of the array which is greater
161 C than T
162 C
163  50 k = k+1
164  if (dx(k) .lt. t) go to 50
165 C
166 C Interchange these elements
167 C
168  if (k .le. l) then
169  tt = dx(l)
170  dx(l) = dx(k)
171  dx(k) = tt
172  go to 40
173  endif
174 C
175 C Save upper and lower subscripts of the array yet to be sorted
176 C
177  if (l-i .gt. j-k) then
178  il(m) = i
179  iu(m) = l
180  i = k
181  m = m+1
182  else
183  il(m) = k
184  iu(m) = j
185  j = l
186  m = m+1
187  endif
188  go to 70
189 C
190 C Begin again on another portion of the unsorted array
191 C
192  60 m = m-1
193  if (m .eq. 0) go to 190
194  i = il(m)
195  j = iu(m)
196 C
197  70 if (j-i .ge. 1) go to 30
198  if (i .eq. 1) go to 20
199  i = i-1
200 C
201  80 i = i+1
202  if (i .eq. j) go to 60
203  t = dx(i+1)
204  if (dx(i) .le. t) go to 80
205  k = i
206 C
207  90 dx(k+1) = dx(k)
208  k = k-1
209  if (t .lt. dx(k)) go to 90
210  dx(k+1) = t
211  go to 80
212 C
213 C Sort DX and carry IY along
214 C
215  100 m = 1
216  i = 1
217  j = nn
218  r = 0.375d0
219 C
220  110 if (i .eq. j) go to 150
221  if (r .le. 0.5898437d0) then
222  r = r+3.90625d-2
223  else
224  r = r-0.21875d0
225  endif
226 C
227  120 k = i
228 C
229 C Select a central element of the array and save it in location T
230 C
231  ij = i + int((j-i)*r)
232  t = dx(ij)
233  ty = iy(ij)
234 C
235 C If first element of array is greater than T, interchange with T
236 C
237  if (dx(i) .gt. t) then
238  dx(ij) = dx(i)
239  dx(i) = t
240  t = dx(ij)
241  iy(ij) = iy(i)
242  iy(i) = ty
243  ty = iy(ij)
244  endif
245  l = j
246 C
247 C If last element of array is less than T, interchange with T
248 C
249  if (dx(j) .lt. t) then
250  dx(ij) = dx(j)
251  dx(j) = t
252  t = dx(ij)
253  iy(ij) = iy(j)
254  iy(j) = ty
255  ty = iy(ij)
256 C
257 C If first element of array is greater than T, interchange with T
258 C
259  if (dx(i) .gt. t) then
260  dx(ij) = dx(i)
261  dx(i) = t
262  t = dx(ij)
263  iy(ij) = iy(i)
264  iy(i) = ty
265  ty = iy(ij)
266  endif
267  endif
268 C
269 C Find an element in the second half of the array which is smaller
270 C than T
271 C
272  130 l = l-1
273  if (dx(l) .gt. t) go to 130
274 C
275 C Find an element in the first half of the array which is greater
276 C than T
277 C
278  140 k = k+1
279  if (dx(k) .lt. t) go to 140
280 C
281 C Interchange these elements
282 C
283  if (k .le. l) then
284  tt = dx(l)
285  dx(l) = dx(k)
286  dx(k) = tt
287  tty = iy(l)
288  iy(l) = iy(k)
289  iy(k) = tty
290  go to 130
291  endif
292 C
293 C Save upper and lower subscripts of the array yet to be sorted
294 C
295  if (l-i .gt. j-k) then
296  il(m) = i
297  iu(m) = l
298  i = k
299  m = m+1
300  else
301  il(m) = k
302  iu(m) = j
303  j = l
304  m = m+1
305  endif
306  go to 160
307 C
308 C Begin again on another portion of the unsorted array
309 C
310  150 m = m-1
311  if (m .eq. 0) go to 190
312  i = il(m)
313  j = iu(m)
314 C
315  160 if (j-i .ge. 1) go to 120
316  if (i .eq. 1) go to 110
317  i = i-1
318 C
319  170 i = i+1
320  if (i .eq. j) go to 150
321  t = dx(i+1)
322  ty = iy(i+1)
323  if (dx(i) .le. t) go to 170
324  k = i
325 C
326  180 dx(k+1) = dx(k)
327  iy(k+1) = iy(k)
328  k = k-1
329  if (t .lt. dx(k)) go to 180
330  dx(k+1) = t
331  iy(k+1) = ty
332  go to 170
333 C
334 C Clean up
335 C
336  190 if (kflag .le. -1) then
337  do 200 i=1,nn
338  dx(i) = -dx(i)
339  200 continue
340  endif
341  return
Hosted by OpenAircraft.com, (Michigan UAV, LLC)