qs2i1d.f 7.0 KB

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