isort.f 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. *DECK ISORT
  2. SUBROUTINE ISORT (IX, IY, N, KFLAG)
  3. C***BEGIN PROLOGUE ISORT
  4. C***PURPOSE Sort an array and optionally make the same interchanges in
  5. C an auxiliary array. The array may be sorted in increasing
  6. C or decreasing order. A slightly modified QUICKSORT
  7. C algorithm is used.
  8. C***LIBRARY SLATEC
  9. C***CATEGORY N6A2A
  10. C***TYPE INTEGER (SSORT-S, DSORT-D, ISORT-I)
  11. C***KEYWORDS SINGLETON QUICKSORT, SORT, SORTING
  12. C***AUTHOR Jones, R. E., (SNLA)
  13. C Kahaner, D. K., (NBS)
  14. C Wisniewski, J. A., (SNLA)
  15. C***DESCRIPTION
  16. C
  17. C ISORT sorts array IX and optionally makes the same interchanges in
  18. C array IY. The array IX may be sorted in increasing order or
  19. C decreasing order. A slightly modified quicksort algorithm is used.
  20. C
  21. C Description of Parameters
  22. C IX - integer array of values to be sorted
  23. C IY - integer array to be (optionally) carried along
  24. C N - number of values in integer array IX to be sorted
  25. C KFLAG - control parameter
  26. C = 2 means sort IX in increasing order and carry IY along.
  27. C = 1 means sort IX in increasing order (ignoring IY)
  28. C = -1 means sort IX in decreasing order (ignoring IY)
  29. C = -2 means sort IX in decreasing order and carry IY along.
  30. C
  31. C***REFERENCES R. C. Singleton, Algorithm 347, An efficient algorithm
  32. C for sorting with minimal storage, Communications of
  33. C the ACM, 12, 3 (1969), pp. 185-187.
  34. C***ROUTINES CALLED XERMSG
  35. C***REVISION HISTORY (YYMMDD)
  36. C 761118 DATE WRITTEN
  37. C 810801 Modified by David K. Kahaner.
  38. C 890531 Changed all specific intrinsics to generic. (WRB)
  39. C 890831 Modified array declarations. (WRB)
  40. C 891009 Removed unreferenced statement labels. (WRB)
  41. C 891009 REVISION DATE from Version 3.2
  42. C 891214 Prologue converted to Version 4.0 format. (BAB)
  43. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  44. C 901012 Declared all variables; changed X,Y to IX,IY. (M. McClain)
  45. C 920501 Reformatted the REFERENCES section. (DWL, WRB)
  46. C 920519 Clarified error messages. (DWL)
  47. C 920801 Declarations section rebuilt and code restructured to use
  48. C IF-THEN-ELSE-ENDIF. (RWC, WRB)
  49. C***END PROLOGUE ISORT
  50. C .. Scalar Arguments ..
  51. INTEGER KFLAG, N
  52. C .. Array Arguments ..
  53. INTEGER IX(*), IY(*)
  54. C .. Local Scalars ..
  55. REAL R
  56. INTEGER I, IJ, J, K, KK, L, M, NN, T, TT, TTY, TY
  57. C .. Local Arrays ..
  58. INTEGER IL(21), IU(21)
  59. C .. External Subroutines ..
  60. EXTERNAL XERMSG
  61. C .. Intrinsic Functions ..
  62. INTRINSIC ABS, INT
  63. C***FIRST EXECUTABLE STATEMENT ISORT
  64. NN = N
  65. IF (NN .LT. 1) THEN
  66. CALL XERMSG ('SLATEC', 'ISORT',
  67. + 'The number of values to be sorted is not positive.', 1, 1)
  68. RETURN
  69. ENDIF
  70. C
  71. KK = ABS(KFLAG)
  72. IF (KK.NE.1 .AND. KK.NE.2) THEN
  73. CALL XERMSG ('SLATEC', 'ISORT',
  74. + 'The sort control parameter, K, is not 2, 1, -1, or -2.', 2,
  75. + 1)
  76. RETURN
  77. ENDIF
  78. C
  79. C Alter array IX to get decreasing order if needed
  80. C
  81. IF (KFLAG .LE. -1) THEN
  82. DO 10 I=1,NN
  83. IX(I) = -IX(I)
  84. 10 CONTINUE
  85. ENDIF
  86. C
  87. IF (KK .EQ. 2) GO TO 100
  88. C
  89. C Sort IX only
  90. C
  91. M = 1
  92. I = 1
  93. J = NN
  94. R = 0.375E0
  95. C
  96. 20 IF (I .EQ. J) GO TO 60
  97. IF (R .LE. 0.5898437E0) THEN
  98. R = R+3.90625E-2
  99. ELSE
  100. R = R-0.21875E0
  101. ENDIF
  102. C
  103. 30 K = I
  104. C
  105. C Select a central element of the array and save it in location T
  106. C
  107. IJ = I + INT((J-I)*R)
  108. T = IX(IJ)
  109. C
  110. C If first element of array is greater than T, interchange with T
  111. C
  112. IF (IX(I) .GT. T) THEN
  113. IX(IJ) = IX(I)
  114. IX(I) = T
  115. T = IX(IJ)
  116. ENDIF
  117. L = J
  118. C
  119. C If last element of array is less than than T, interchange with T
  120. C
  121. IF (IX(J) .LT. T) THEN
  122. IX(IJ) = IX(J)
  123. IX(J) = T
  124. T = IX(IJ)
  125. C
  126. C If first element of array is greater than T, interchange with T
  127. C
  128. IF (IX(I) .GT. T) THEN
  129. IX(IJ) = IX(I)
  130. IX(I) = T
  131. T = IX(IJ)
  132. ENDIF
  133. ENDIF
  134. C
  135. C Find an element in the second half of the array which is smaller
  136. C than T
  137. C
  138. 40 L = L-1
  139. IF (IX(L) .GT. T) GO TO 40
  140. C
  141. C Find an element in the first half of the array which is greater
  142. C than T
  143. C
  144. 50 K = K+1
  145. IF (IX(K) .LT. T) GO TO 50
  146. C
  147. C Interchange these elements
  148. C
  149. IF (K .LE. L) THEN
  150. TT = IX(L)
  151. IX(L) = IX(K)
  152. IX(K) = TT
  153. GO TO 40
  154. ENDIF
  155. C
  156. C Save upper and lower subscripts of the array yet to be sorted
  157. C
  158. IF (L-I .GT. J-K) THEN
  159. IL(M) = I
  160. IU(M) = L
  161. I = K
  162. M = M+1
  163. ELSE
  164. IL(M) = K
  165. IU(M) = J
  166. J = L
  167. M = M+1
  168. ENDIF
  169. GO TO 70
  170. C
  171. C Begin again on another portion of the unsorted array
  172. C
  173. 60 M = M-1
  174. IF (M .EQ. 0) GO TO 190
  175. I = IL(M)
  176. J = IU(M)
  177. C
  178. 70 IF (J-I .GE. 1) GO TO 30
  179. IF (I .EQ. 1) GO TO 20
  180. I = I-1
  181. C
  182. 80 I = I+1
  183. IF (I .EQ. J) GO TO 60
  184. T = IX(I+1)
  185. IF (IX(I) .LE. T) GO TO 80
  186. K = I
  187. C
  188. 90 IX(K+1) = IX(K)
  189. K = K-1
  190. IF (T .LT. IX(K)) GO TO 90
  191. IX(K+1) = T
  192. GO TO 80
  193. C
  194. C Sort IX and carry IY along
  195. C
  196. 100 M = 1
  197. I = 1
  198. J = NN
  199. R = 0.375E0
  200. C
  201. 110 IF (I .EQ. J) GO TO 150
  202. IF (R .LE. 0.5898437E0) THEN
  203. R = R+3.90625E-2
  204. ELSE
  205. R = R-0.21875E0
  206. ENDIF
  207. C
  208. 120 K = I
  209. C
  210. C Select a central element of the array and save it in location T
  211. C
  212. IJ = I + INT((J-I)*R)
  213. T = IX(IJ)
  214. TY = IY(IJ)
  215. C
  216. C If first element of array is greater than T, interchange with T
  217. C
  218. IF (IX(I) .GT. T) THEN
  219. IX(IJ) = IX(I)
  220. IX(I) = T
  221. T = IX(IJ)
  222. IY(IJ) = IY(I)
  223. IY(I) = TY
  224. TY = IY(IJ)
  225. ENDIF
  226. L = J
  227. C
  228. C If last element of array is less than T, interchange with T
  229. C
  230. IF (IX(J) .LT. T) THEN
  231. IX(IJ) = IX(J)
  232. IX(J) = T
  233. T = IX(IJ)
  234. IY(IJ) = IY(J)
  235. IY(J) = TY
  236. TY = IY(IJ)
  237. C
  238. C If first element of array is greater than T, interchange with T
  239. C
  240. IF (IX(I) .GT. T) THEN
  241. IX(IJ) = IX(I)
  242. IX(I) = T
  243. T = IX(IJ)
  244. IY(IJ) = IY(I)
  245. IY(I) = TY
  246. TY = IY(IJ)
  247. ENDIF
  248. ENDIF
  249. C
  250. C Find an element in the second half of the array which is smaller
  251. C than T
  252. C
  253. 130 L = L-1
  254. IF (IX(L) .GT. T) GO TO 130
  255. C
  256. C Find an element in the first half of the array which is greater
  257. C than T
  258. C
  259. 140 K = K+1
  260. IF (IX(K) .LT. T) GO TO 140
  261. C
  262. C Interchange these elements
  263. C
  264. IF (K .LE. L) THEN
  265. TT = IX(L)
  266. IX(L) = IX(K)
  267. IX(K) = TT
  268. TTY = IY(L)
  269. IY(L) = IY(K)
  270. IY(K) = TTY
  271. GO TO 130
  272. ENDIF
  273. C
  274. C Save upper and lower subscripts of the array yet to be sorted
  275. C
  276. IF (L-I .GT. J-K) THEN
  277. IL(M) = I
  278. IU(M) = L
  279. I = K
  280. M = M+1
  281. ELSE
  282. IL(M) = K
  283. IU(M) = J
  284. J = L
  285. M = M+1
  286. ENDIF
  287. GO TO 160
  288. C
  289. C Begin again on another portion of the unsorted array
  290. C
  291. 150 M = M-1
  292. IF (M .EQ. 0) GO TO 190
  293. I = IL(M)
  294. J = IU(M)
  295. C
  296. 160 IF (J-I .GE. 1) GO TO 120
  297. IF (I .EQ. 1) GO TO 110
  298. I = I-1
  299. C
  300. 170 I = I+1
  301. IF (I .EQ. J) GO TO 150
  302. T = IX(I+1)
  303. TY = IY(I+1)
  304. IF (IX(I) .LE. T) GO TO 170
  305. K = I
  306. C
  307. 180 IX(K+1) = IX(K)
  308. IY(K+1) = IY(K)
  309. K = K-1
  310. IF (T .LT. IX(K)) GO TO 180
  311. IX(K+1) = T
  312. IY(K+1) = TY
  313. GO TO 170
  314. C
  315. C Clean up
  316. C
  317. 190 IF (KFLAG .LE. -1) THEN
  318. DO 200 I=1,NN
  319. IX(I) = -IX(I)
  320. 200 CONTINUE
  321. ENDIF
  322. RETURN
  323. END