1
SUBROUTINE DSORT (DX, IX, N, KFLAG, IERR)
2
C***BEGIN PROLOGUE DSORT
3
C***PURPOSE Sort an array and optionally make the same interchanges in
4
C an auxiliary array. The array may be sorted in increasing
5
C or decreasing order. A slightly modified QUICKSORT
9
C***TYPE real (SSORT-S, DSORT-D, ISORT-I)
10
C***KEYWORDS SINGLETON QUICKSORT, SORT, SORTING
11
C***AUTHOR Jones, R. E., (SNLA)
12
C Wisniewski, J. A., (SNLA)
15
C DSORT sorts array DX and optionally makes the same interchanges in
16
C array IX. The array DX may be sorted in increasing order or
17
C decreasing order. A slightly modified quicksort algorithm is used.
19
C Description of Parameters
20
C DX - array of values to be sorted (usually abscissas)
21
C IX - array to be (optionally) carried along
22
C N - number of values in array DX to be sorted
23
C KFLAG - control parameter
24
C = 2 means sort DX in increasing order and carry IX along.
25
C = 1 means sort DX in increasing order (ignoring IX)
26
C = -1 means sort DX in decreasing order (ignoring IX)
27
C = -2 means sort DX in decreasing order and carry IX along.
29
C IERR - the error's indicator
33
C***REFERENCES R. C. Singleton, Algorithm 347, An efficient algorithm
34
C for sorting with minimal storage, Communications of
35
C the ACM, 12, 3 (1969), pp. 185-187.
36
C***ROUTINES CALLED XERMSG
37
C***REVISION HISTORY (YYMMDD)
39
C 761118 Modified to use the Singleton quicksort algorithm. (JAW)
40
C 890531 Changed all specific intrinsics to generic. (WRB)
41
C 890831 Modified array declarations. (WRB)
42
C 891009 Removed unreferenced statement labels. (WRB)
43
C 891024 Changed category. (WRB)
44
C 891024 REVISION DATE from Version 3.2
45
C 891214 Prologue converted to Version 4.0 format. (BAB)
46
C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
47
C 901012 Declared all variables; changed X,Y to DX,IX; changed
48
C code to parallel SSORT. (M. McClain)
49
C 920501 Reformatted the REFERENCES section. (DWL, WRB)
50
C 920519 Clarified error messages. (DWL)
51
C 920801 Declarations section rebuilt and code restructured to use
52
C IF-THEN-ELSE-ENDIF. (RWC, WRB)
53
C***END PROLOGUE DSORT
54
C .. Scalar Arguments ..
56
C .. Array Arguments ..
61
INTEGER I, IJ, J, K, KK, L, M, NN
63
INTEGER IL(21), IU(21)
64
C .. Intrinsic Functions ..
66
C***FIRST EXECUTABLE STATEMENT DSORT
76
IF (KK.NE.1 .AND. KK.NE.2) THEN
88
C Alter array DX to get decreasing order if needed
90
IF (KFLAG .LE. -1) THEN
96
IF (KK .EQ. 2) GO TO 100
105
20 IF (I .EQ. J) GO TO 60
106
IF (R .LE. 0.5898437D0) THEN
114
C Select a central element of the array and save it in location T
116
IJ = I + INT((J-I)*R)
119
C If first element of array is greater than T, interchange with T
121
IF (DX(I) .GT. T) THEN
128
C If last element of array is less than than T, interchange with T
130
IF (DX(J) .LT. T) THEN
135
C If first element of array is greater than T, interchange with T
137
IF (DX(I) .GT. T) THEN
144
C Find an element in the second half of the array which is smaller
148
IF (DX(L) .GT. T) GO TO 40
150
C Find an element in the first half of the array which is greater
154
IF (DX(K) .LT. T) GO TO 50
156
C Interchange these elements
165
C Save upper and lower subscripts of the array yet to be sorted
167
IF (L-I .GT. J-K) THEN
180
C Begin again on another portion of the unsorted array
183
IF (M .EQ. 0) GO TO 190
187
70 IF (J-I .GE. 1) GO TO 30
188
IF (I .EQ. 1) GO TO 20
192
IF (I .EQ. J) GO TO 60
194
IF (DX(I) .LE. T) GO TO 80
199
IF (T .LT. DX(K)) GO TO 90
203
C Sort DX and carry IX along
210
110 IF (I .EQ. J) GO TO 150
211
IF (R .LE. 0.5898437D0) THEN
219
C Select a central element of the array and save it in location T
221
IJ = I + INT((J-I)*R)
225
C If first element of array is greater than T, interchange with T
227
IF (DX(I) .GT. T) THEN
237
C If last element of array is less than T, interchange with T
239
IF (DX(J) .LT. T) THEN
247
C If first element of array is greater than T, interchange with T
249
IF (DX(I) .GT. T) THEN
259
C Find an element in the second half of the array which is smaller
263
IF (DX(L) .GT. T) GO TO 130
265
C Find an element in the first half of the array which is greater
269
IF (DX(K) .LT. T) GO TO 140
271
C Interchange these elements
283
C Save upper and lower subscripts of the array yet to be sorted
285
IF (L-I .GT. J-K) THEN
298
C Begin again on another portion of the unsorted array
301
IF (M .EQ. 0) GO TO 190
305
160 IF (J-I .GE. 1) GO TO 120
306
IF (I .EQ. 1) GO TO 110
310
IF (I .EQ. J) GO TO 150
313
IF (DX(I) .LE. T) GO TO 170
319
IF (T .LT. DX(K)) GO TO 180
326
190 IF (KFLAG .LE. -1) THEN