qs2i1r.f 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. *DECK QS2I1R
  2. SUBROUTINE QS2I1R (IA, JA, A, N, KFLAG)
  3. C***BEGIN PROLOGUE QS2I1R
  4. C***SUBSIDIARY
  5. C***PURPOSE Sort an integer array, moving an integer and real array.
  6. C This routine sorts the integer array IA and makes the same
  7. C interchanges in the integer array JA and the real array A.
  8. C The array IA may be sorted in increasing order or decreas-
  9. C ing order. A slightly modified QUICKSORT algorithm is
  10. C used.
  11. C***LIBRARY SLATEC (SLAP)
  12. C***CATEGORY N6A2A
  13. C***TYPE SINGLE PRECISION (QS2I1R-S, QS2I1D-D)
  14. C***KEYWORDS SINGLETON QUICKSORT, SLAP, SORT, SORTING
  15. C***AUTHOR Jones, R. E., (SNLA)
  16. C Kahaner, D. K., (NBS)
  17. C Seager, M. K., (LLNL) seager@llnl.gov
  18. C Wisniewski, J. A., (SNLA)
  19. C***DESCRIPTION
  20. C Written by Rondall E Jones
  21. C Modified by John A. Wisniewski to use the Singleton QUICKSORT
  22. C algorithm. date 18 November 1976.
  23. C
  24. C Further modified by David K. Kahaner
  25. C National Bureau of Standards
  26. C August, 1981
  27. C
  28. C Even further modification made to bring the code up to the
  29. C Fortran 77 level and make it more readable and to carry
  30. C along one integer array and one real array during the sort by
  31. C Mark K. Seager
  32. C Lawrence Livermore National Laboratory
  33. C November, 1987
  34. C This routine was adapted from the ISORT routine.
  35. C
  36. C ABSTRACT
  37. C This routine sorts an integer array IA and makes the same
  38. C interchanges in the integer array JA and the real array A.
  39. C The array IA may be sorted in increasing order or decreasing
  40. C order. A slightly modified quicksort algorithm is used.
  41. C
  42. C DESCRIPTION OF PARAMETERS
  43. C IA - Integer array of values to be sorted.
  44. C JA - Integer array to be carried along.
  45. C A - Real array to be carried along.
  46. C N - Number of values in integer array IA to be sorted.
  47. C KFLAG - Control parameter
  48. C = 1 means sort IA in INCREASING order.
  49. C =-1 means sort IA in DECREASING order.
  50. C
  51. C***SEE ALSO SS2Y
  52. C***REFERENCES R. C. Singleton, Algorithm 347, An Efficient Algorithm
  53. C for Sorting With Minimal Storage, Communications ACM
  54. C 12:3 (1969), pp.185-7.
  55. C***ROUTINES CALLED XERMSG
  56. C***REVISION HISTORY (YYMMDD)
  57. C 761118 DATE WRITTEN
  58. C 890125 Previous REVISION DATE
  59. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  60. C 890922 Numerous changes to prologue to make closer to SLATEC
  61. C standard. (FNF)
  62. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  63. C 900805 Changed XERROR calls to calls to XERMSG. (RWC)
  64. C 910411 Prologue converted to Version 4.0 format. (BAB)
  65. C 910506 Made subsidiary to SS2Y and corrected reference. (FNF)
  66. C 920511 Added complete declaration section. (WRB)
  67. C 920929 Corrected format of reference. (FNF)
  68. C 921012 Added E0's to f.p. constants. (FNF)
  69. C***END PROLOGUE QS2I1R
  70. CVD$R NOVECTOR
  71. CVD$R NOCONCUR
  72. C .. Scalar Arguments ..
  73. INTEGER KFLAG, N
  74. C .. Array Arguments ..
  75. REAL A(N)
  76. INTEGER IA(N), JA(N)
  77. C .. Local Scalars ..
  78. REAL R, TA, TTA
  79. INTEGER I, IIT, IJ, IT, J, JJT, JT, K, KK, L, M, NN
  80. C .. Local Arrays ..
  81. INTEGER IL(21), IU(21)
  82. C .. External Subroutines ..
  83. EXTERNAL XERMSG
  84. C .. Intrinsic Functions ..
  85. INTRINSIC ABS, INT
  86. C***FIRST EXECUTABLE STATEMENT QS2I1R
  87. NN = N
  88. IF (NN.LT.1) THEN
  89. CALL XERMSG ('SLATEC', 'QS2I1R',
  90. $ 'The number of values to be sorted was not positive.', 1, 1)
  91. RETURN
  92. ENDIF
  93. IF( N.EQ.1 ) RETURN
  94. KK = ABS(KFLAG)
  95. IF ( KK.NE.1 ) THEN
  96. CALL XERMSG ('SLATEC', 'QS2I1R',
  97. $ 'The sort control parameter, K, was not 1 or -1.', 2, 1)
  98. RETURN
  99. ENDIF
  100. C
  101. C Alter array IA to get decreasing order if needed.
  102. C
  103. IF( KFLAG.LT.1 ) THEN
  104. DO 20 I=1,NN
  105. IA(I) = -IA(I)
  106. 20 CONTINUE
  107. ENDIF
  108. C
  109. C Sort IA and carry JA and A along.
  110. C And now...Just a little black magic...
  111. M = 1
  112. I = 1
  113. J = NN
  114. R = .375E0
  115. 210 IF( R.LE.0.5898437E0 ) THEN
  116. R = R + 3.90625E-2
  117. ELSE
  118. R = R-.21875E0
  119. ENDIF
  120. 225 K = I
  121. C
  122. C Select a central element of the array and save it in location
  123. C it, jt, at.
  124. C
  125. IJ = I + INT ((J-I)*R)
  126. IT = IA(IJ)
  127. JT = JA(IJ)
  128. TA = A(IJ)
  129. C
  130. C If first element of array is greater than it, interchange with it.
  131. C
  132. IF( IA(I).GT.IT ) THEN
  133. IA(IJ) = IA(I)
  134. IA(I) = IT
  135. IT = IA(IJ)
  136. JA(IJ) = JA(I)
  137. JA(I) = JT
  138. JT = JA(IJ)
  139. A(IJ) = A(I)
  140. A(I) = TA
  141. TA = A(IJ)
  142. ENDIF
  143. L=J
  144. C
  145. C If last element of array is less than it, swap with it.
  146. C
  147. IF( IA(J).LT.IT ) THEN
  148. IA(IJ) = IA(J)
  149. IA(J) = IT
  150. IT = IA(IJ)
  151. JA(IJ) = JA(J)
  152. JA(J) = JT
  153. JT = JA(IJ)
  154. A(IJ) = A(J)
  155. A(J) = TA
  156. TA = A(IJ)
  157. C
  158. C If first element of array is greater than it, swap with it.
  159. C
  160. IF ( IA(I).GT.IT ) THEN
  161. IA(IJ) = IA(I)
  162. IA(I) = IT
  163. IT = IA(IJ)
  164. JA(IJ) = JA(I)
  165. JA(I) = JT
  166. JT = JA(IJ)
  167. A(IJ) = A(I)
  168. A(I) = TA
  169. TA = A(IJ)
  170. ENDIF
  171. ENDIF
  172. C
  173. C Find an element in the second half of the array which is
  174. C smaller than it.
  175. C
  176. 240 L=L-1
  177. IF( IA(L).GT.IT ) GO TO 240
  178. C
  179. C Find an element in the first half of the array which is
  180. C greater than it.
  181. C
  182. 245 K=K+1
  183. IF( IA(K).LT.IT ) GO TO 245
  184. C
  185. C Interchange these elements.
  186. C
  187. IF( K.LE.L ) THEN
  188. IIT = IA(L)
  189. IA(L) = IA(K)
  190. IA(K) = IIT
  191. JJT = JA(L)
  192. JA(L) = JA(K)
  193. JA(K) = JJT
  194. TTA = A(L)
  195. A(L) = A(K)
  196. A(K) = TTA
  197. GOTO 240
  198. ENDIF
  199. C
  200. C Save upper and lower subscripts of the array yet to be sorted.
  201. C
  202. IF( L-I.GT.J-K ) THEN
  203. IL(M) = I
  204. IU(M) = L
  205. I = K
  206. M = M+1
  207. ELSE
  208. IL(M) = K
  209. IU(M) = J
  210. J = L
  211. M = M+1
  212. ENDIF
  213. GO TO 260
  214. C
  215. C Begin again on another portion of the unsorted array.
  216. C
  217. 255 M = M-1
  218. IF( M.EQ.0 ) GO TO 300
  219. I = IL(M)
  220. J = IU(M)
  221. 260 IF( J-I.GE.1 ) GO TO 225
  222. IF( I.EQ.J ) GO TO 255
  223. IF( I.EQ.1 ) GO TO 210
  224. I = I-1
  225. 265 I = I+1
  226. IF( I.EQ.J ) GO TO 255
  227. IT = IA(I+1)
  228. JT = JA(I+1)
  229. TA = A(I+1)
  230. IF( IA(I).LE.IT ) GO TO 265
  231. K=I
  232. 270 IA(K+1) = IA(K)
  233. JA(K+1) = JA(K)
  234. A(K+1) = A(K)
  235. K = K-1
  236. IF( IT.LT.IA(K) ) GO TO 270
  237. IA(K+1) = IT
  238. JA(K+1) = JT
  239. A(K+1) = TA
  240. GO TO 265
  241. C
  242. C Clean up, if necessary.
  243. C
  244. 300 IF( KFLAG.LT.1 ) THEN
  245. DO 310 I=1,NN
  246. IA(I) = -IA(I)
  247. 310 CONTINUE
  248. ENDIF
  249. RETURN
  250. C------------- LAST LINE OF QS2I1R FOLLOWS ----------------------------
  251. END