LAPACK 3.12.1
LAPACK: Linear Algebra PACKage
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
slasrt.f
Go to the documentation of this file.
1*> \brief \b SLASRT sorts numbers in increasing or decreasing order.
2*
3* =========== DOCUMENTATION ===========
4*
5* Online html documentation available at
6* http://www.netlib.org/lapack/explore-html/
7*
8*> Download SLASRT + dependencies
9*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/slasrt.f">
10*> [TGZ]</a>
11*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/slasrt.f">
12*> [ZIP]</a>
13*> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/slasrt.f">
14*> [TXT]</a>
15*
16* Definition:
17* ===========
18*
19* SUBROUTINE SLASRT( ID, N, D, INFO )
20*
21* .. Scalar Arguments ..
22* CHARACTER ID
23* INTEGER INFO, N
24* ..
25* .. Array Arguments ..
26* REAL D( * )
27* ..
28*
29*
30*> \par Purpose:
31* =============
32*>
33*> \verbatim
34*>
35*> Sort the numbers in D in increasing order (if ID = 'I') or
36*> in decreasing order (if ID = 'D' ).
37*>
38*> Use Quick Sort, reverting to Insertion sort on arrays of
39*> size <= 20. Dimension of STACK limits N to about 2**32.
40*> \endverbatim
41*
42* Arguments:
43* ==========
44*
45*> \param[in] ID
46*> \verbatim
47*> ID is CHARACTER*1
48*> = 'I': sort D in increasing order;
49*> = 'D': sort D in decreasing order.
50*> \endverbatim
51*>
52*> \param[in] N
53*> \verbatim
54*> N is INTEGER
55*> The length of the array D.
56*> \endverbatim
57*>
58*> \param[in,out] D
59*> \verbatim
60*> D is REAL array, dimension (N)
61*> On entry, the array to be sorted.
62*> On exit, D has been sorted into increasing order
63*> (D(1) <= ... <= D(N) ) or into decreasing order
64*> (D(1) >= ... >= D(N) ), depending on ID.
65*> \endverbatim
66*>
67*> \param[out] INFO
68*> \verbatim
69*> INFO is INTEGER
70*> = 0: successful exit
71*> < 0: if INFO = -i, the i-th argument had an illegal value
72*> \endverbatim
73*
74* Authors:
75* ========
76*
77*> \author Univ. of Tennessee
78*> \author Univ. of California Berkeley
79*> \author Univ. of Colorado Denver
80*> \author NAG Ltd.
81*
82*> \ingroup lasrt
83*
84* =====================================================================
85 SUBROUTINE slasrt( ID, N, D, INFO )
86*
87* -- LAPACK computational routine --
88* -- LAPACK is a software package provided by Univ. of Tennessee, --
89* -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
90*
91* .. Scalar Arguments ..
92 CHARACTER ID
93 INTEGER INFO, N
94* ..
95* .. Array Arguments ..
96 REAL D( * )
97* ..
98*
99* =====================================================================
100*
101* .. Parameters ..
102 INTEGER SELECT
103 parameter( SELECT = 20 )
104* ..
105* .. Local Scalars ..
106 INTEGER DIR, ENDD, I, J, START, STKPNT
107 REAL D1, D2, D3, DMNMX, TMP
108* ..
109* .. Local Arrays ..
110 INTEGER STACK( 2, 32 )
111* ..
112* .. External Functions ..
113 LOGICAL LSAME
114 EXTERNAL lsame
115* ..
116* .. External Subroutines ..
117 EXTERNAL xerbla
118* ..
119* .. Executable Statements ..
120*
121* Test the input parameters.
122*
123 info = 0
124 dir = -1
125 IF( lsame( id, 'D' ) ) THEN
126 dir = 0
127 ELSE IF( lsame( id, 'I' ) ) THEN
128 dir = 1
129 END IF
130 IF( dir.EQ.-1 ) THEN
131 info = -1
132 ELSE IF( n.LT.0 ) THEN
133 info = -2
134 END IF
135 IF( info.NE.0 ) THEN
136 CALL xerbla( 'SLASRT', -info )
137 RETURN
138 END IF
139*
140* Quick return if possible
141*
142 IF( n.LE.1 )
143 $ RETURN
144*
145 stkpnt = 1
146 stack( 1, 1 ) = 1
147 stack( 2, 1 ) = n
148 10 CONTINUE
149 start = stack( 1, stkpnt )
150 endd = stack( 2, stkpnt )
151 stkpnt = stkpnt - 1
152 IF( endd-start.LE.SELECT .AND. endd-start.GT.0 ) THEN
153*
154* Do Insertion sort on D( START:ENDD )
155*
156 IF( dir.EQ.0 ) THEN
157*
158* Sort into decreasing order
159*
160 DO 30 i = start + 1, endd
161 DO 20 j = i, start + 1, -1
162 IF( d( j ).GT.d( j-1 ) ) THEN
163 dmnmx = d( j )
164 d( j ) = d( j-1 )
165 d( j-1 ) = dmnmx
166 ELSE
167 GO TO 30
168 END IF
169 20 CONTINUE
170 30 CONTINUE
171*
172 ELSE
173*
174* Sort into increasing order
175*
176 DO 50 i = start + 1, endd
177 DO 40 j = i, start + 1, -1
178 IF( d( j ).LT.d( j-1 ) ) THEN
179 dmnmx = d( j )
180 d( j ) = d( j-1 )
181 d( j-1 ) = dmnmx
182 ELSE
183 GO TO 50
184 END IF
185 40 CONTINUE
186 50 CONTINUE
187*
188 END IF
189*
190 ELSE IF( endd-start.GT.SELECT ) THEN
191*
192* Partition D( START:ENDD ) and stack parts, largest one first
193*
194* Choose partition entry as median of 3
195*
196 d1 = d( start )
197 d2 = d( endd )
198 i = ( start+endd ) / 2
199 d3 = d( i )
200 IF( d1.LT.d2 ) THEN
201 IF( d3.LT.d1 ) THEN
202 dmnmx = d1
203 ELSE IF( d3.LT.d2 ) THEN
204 dmnmx = d3
205 ELSE
206 dmnmx = d2
207 END IF
208 ELSE
209 IF( d3.LT.d2 ) THEN
210 dmnmx = d2
211 ELSE IF( d3.LT.d1 ) THEN
212 dmnmx = d3
213 ELSE
214 dmnmx = d1
215 END IF
216 END IF
217*
218 IF( dir.EQ.0 ) THEN
219*
220* Sort into decreasing order
221*
222 i = start - 1
223 j = endd + 1
224 60 CONTINUE
225 70 CONTINUE
226 j = j - 1
227 IF( d( j ).LT.dmnmx )
228 $ GO TO 70
229 80 CONTINUE
230 i = i + 1
231 IF( d( i ).GT.dmnmx )
232 $ GO TO 80
233 IF( i.LT.j ) THEN
234 tmp = d( i )
235 d( i ) = d( j )
236 d( j ) = tmp
237 GO TO 60
238 END IF
239 IF( j-start.GT.endd-j-1 ) THEN
240 stkpnt = stkpnt + 1
241 stack( 1, stkpnt ) = start
242 stack( 2, stkpnt ) = j
243 stkpnt = stkpnt + 1
244 stack( 1, stkpnt ) = j + 1
245 stack( 2, stkpnt ) = endd
246 ELSE
247 stkpnt = stkpnt + 1
248 stack( 1, stkpnt ) = j + 1
249 stack( 2, stkpnt ) = endd
250 stkpnt = stkpnt + 1
251 stack( 1, stkpnt ) = start
252 stack( 2, stkpnt ) = j
253 END IF
254 ELSE
255*
256* Sort into increasing order
257*
258 i = start - 1
259 j = endd + 1
260 90 CONTINUE
261 100 CONTINUE
262 j = j - 1
263 IF( d( j ).GT.dmnmx )
264 $ GO TO 100
265 110 CONTINUE
266 i = i + 1
267 IF( d( i ).LT.dmnmx )
268 $ GO TO 110
269 IF( i.LT.j ) THEN
270 tmp = d( i )
271 d( i ) = d( j )
272 d( j ) = tmp
273 GO TO 90
274 END IF
275 IF( j-start.GT.endd-j-1 ) THEN
276 stkpnt = stkpnt + 1
277 stack( 1, stkpnt ) = start
278 stack( 2, stkpnt ) = j
279 stkpnt = stkpnt + 1
280 stack( 1, stkpnt ) = j + 1
281 stack( 2, stkpnt ) = endd
282 ELSE
283 stkpnt = stkpnt + 1
284 stack( 1, stkpnt ) = j + 1
285 stack( 2, stkpnt ) = endd
286 stkpnt = stkpnt + 1
287 stack( 1, stkpnt ) = start
288 stack( 2, stkpnt ) = j
289 END IF
290 END IF
291 END IF
292 IF( stkpnt.GT.0 )
293 $ GO TO 10
294 RETURN
295*
296* End of SLASRT
297*
298 END
subroutine xerbla(srname, info)
Definition cblat2.f:3285
subroutine slasrt(id, n, d, info)
SLASRT sorts numbers in increasing or decreasing order.
Definition slasrt.f:86