root / src / refsor.f90 @ 3
Historique | Voir | Annoter | Télécharger (14,36 ko)
| 1 | 1 | pfleura2 | !!! Downloaded from http://www.fortran-2000.com/rank/index.html |
|---|---|---|---|
| 2 | 1 | pfleura2 | !!! on October 13th, 2010 |
| 3 | 1 | pfleura2 | |
| 4 | 1 | pfleura2 | !PFL 2010 Oct 13-> |
| 5 | 1 | pfleura2 | ! I deleted the 'module' part to keep only |
| 6 | 1 | pfleura2 | ! the Double subroutine. |
| 7 | 1 | pfleura2 | |
| 8 | 1 | pfleura2 | ! Module m_refsor |
| 9 | 1 | pfleura2 | ! ! PFL 2010 Oct 13 -> |
| 10 | 1 | pfleura2 | ! !Integer, Parameter :: kdp = selected_real_kind(15) |
| 11 | 1 | pfleura2 | ! Integer, Parameter :: kdp = KIND(1.0D0) |
| 12 | 1 | pfleura2 | ! ! <- PFL 2010 Oct 13 |
| 13 | 1 | pfleura2 | ! public :: refsor |
| 14 | 1 | pfleura2 | ! private :: kdp |
| 15 | 1 | pfleura2 | ! private :: R_refsor, I_refsor, D_refsor |
| 16 | 1 | pfleura2 | ! private :: R_inssor, I_inssor, D_inssor |
| 17 | 1 | pfleura2 | ! private :: R_subsor, I_subsor, D_subsor |
| 18 | 1 | pfleura2 | ! interface refsor |
| 19 | 1 | pfleura2 | ! module procedure d_refsor, r_refsor, i_refsor |
| 20 | 1 | pfleura2 | ! end interface refsor |
| 21 | 1 | pfleura2 | ! contains |
| 22 | 1 | pfleura2 | |
| 23 | 1 | pfleura2 | ! Subroutine D_refsor (XDONT) |
| 24 | 1 | pfleura2 | Subroutine Drefsor (XDONT) |
| 25 | 1 | pfleura2 | ! Sorts XDONT into ascending order - Quicksort |
| 26 | 1 | pfleura2 | ! __________________________________________________________ |
| 27 | 1 | pfleura2 | ! Quicksort chooses a "pivot" in the set, and explores the |
| 28 | 1 | pfleura2 | ! array from both ends, looking for a value > pivot with the |
| 29 | 1 | pfleura2 | ! increasing index, for a value <= pivot with the decreasing |
| 30 | 1 | pfleura2 | ! index, and swapping them when it has found one of each. |
| 31 | 1 | pfleura2 | ! The array is then subdivided in 2 ([3]) subsets: |
| 32 | 1 | pfleura2 | ! { values <= pivot} {pivot} {values > pivot}
|
| 33 | 1 | pfleura2 | ! One then call recursively the program to sort each subset. |
| 34 | 1 | pfleura2 | ! When the size of the subarray is small enough, one uses an |
| 35 | 1 | pfleura2 | ! insertion sort that is faster for very small sets. |
| 36 | 1 | pfleura2 | ! Michel Olagnon - Apr. 2000 |
| 37 | 1 | pfleura2 | ! __________________________________________________________ |
| 38 | 1 | pfleura2 | ! __________________________________________________________ |
| 39 | 1 | pfleura2 | |
| 40 | 1 | pfleura2 | use VarTypes |
| 41 | 1 | pfleura2 | |
| 42 | 3 | pfleura2 | interface |
| 43 | 3 | pfleura2 | |
| 44 | 3 | pfleura2 | Recursive Subroutine D_subsor (XDONT, IDEB1, IFIN1) |
| 45 | 3 | pfleura2 | |
| 46 | 3 | pfleura2 | use VarTypes |
| 47 | 3 | pfleura2 | |
| 48 | 3 | pfleura2 | IMPLICIT NONE |
| 49 | 3 | pfleura2 | |
| 50 | 3 | pfleura2 | Real(KREAL), dimension (:), Intent (InOut) :: XDONT |
| 51 | 3 | pfleura2 | Integer(KINT), Intent (In) :: IDEB1, IFIN1 |
| 52 | 3 | pfleura2 | |
| 53 | 3 | pfleura2 | Integer(KINT), Parameter :: NINS = 16 ! Max for insertion sort |
| 54 | 3 | pfleura2 | Integer(KINT) :: ICRS, IDEB, IDCR, IFIN, IMIL |
| 55 | 3 | pfleura2 | Real(KREAL) :: XPIV, XWRK |
| 56 | 3 | pfleura2 | end Subroutine D_subsor |
| 57 | 3 | pfleura2 | |
| 58 | 3 | pfleura2 | Subroutine D_inssor (XDONT) |
| 59 | 3 | pfleura2 | |
| 60 | 3 | pfleura2 | use VarTypes |
| 61 | 3 | pfleura2 | |
| 62 | 3 | pfleura2 | IMPLICIT NONE |
| 63 | 3 | pfleura2 | Real(KREAL), dimension (:), Intent (InOut) :: XDONT |
| 64 | 3 | pfleura2 | |
| 65 | 3 | pfleura2 | Integer(KINT) :: ICRS, IDCR |
| 66 | 3 | pfleura2 | Real(KREAL) :: XWRK |
| 67 | 3 | pfleura2 | |
| 68 | 3 | pfleura2 | End Subroutine D_inssor |
| 69 | 3 | pfleura2 | |
| 70 | 3 | pfleura2 | end interface |
| 71 | 3 | pfleura2 | |
| 72 | 1 | pfleura2 | Real (KREAL), Dimension (:), Intent (InOut) :: XDONT |
| 73 | 1 | pfleura2 | ! __________________________________________________________ |
| 74 | 1 | pfleura2 | ! |
| 75 | 1 | pfleura2 | ! |
| 76 | 1 | pfleura2 | Call D_subsor (XDONT, 1, Size (XDONT)) |
| 77 | 1 | pfleura2 | Call D_inssor (XDONT) |
| 78 | 1 | pfleura2 | Return |
| 79 | 1 | pfleura2 | End Subroutine Drefsor |
| 80 | 1 | pfleura2 | |
| 81 | 1 | pfleura2 | Recursive Subroutine D_subsor (XDONT, IDEB1, IFIN1) |
| 82 | 1 | pfleura2 | ! Sorts XDONT from IDEB1 to IFIN1 |
| 83 | 1 | pfleura2 | ! __________________________________________________________ |
| 84 | 1 | pfleura2 | |
| 85 | 1 | pfleura2 | use VarTypes |
| 86 | 1 | pfleura2 | |
| 87 | 1 | pfleura2 | IMPLICIT NONE |
| 88 | 1 | pfleura2 | |
| 89 | 1 | pfleura2 | Real(KREAL), dimension (:), Intent (InOut) :: XDONT |
| 90 | 1 | pfleura2 | Integer(KINT), Intent (In) :: IDEB1, IFIN1 |
| 91 | 1 | pfleura2 | ! __________________________________________________________ |
| 92 | 1 | pfleura2 | Integer(KINT), Parameter :: NINS = 16 ! Max for insertion sort |
| 93 | 1 | pfleura2 | Integer(KINT) :: ICRS, IDEB, IDCR, IFIN, IMIL |
| 94 | 1 | pfleura2 | Real(KREAL) :: XPIV, XWRK |
| 95 | 1 | pfleura2 | ! |
| 96 | 1 | pfleura2 | IDEB = IDEB1 |
| 97 | 1 | pfleura2 | IFIN = IFIN1 |
| 98 | 1 | pfleura2 | ! |
| 99 | 1 | pfleura2 | ! If we don't have enough values to make it worth while, we leave |
| 100 | 1 | pfleura2 | ! them unsorted, and the final insertion sort will take care of them |
| 101 | 1 | pfleura2 | ! |
| 102 | 1 | pfleura2 | If ((IFIN - IDEB) > NINS) Then |
| 103 | 1 | pfleura2 | IMIL = (IDEB+IFIN) / 2 |
| 104 | 1 | pfleura2 | ! |
| 105 | 1 | pfleura2 | ! One chooses a pivot, median of 1st, last, and middle values |
| 106 | 1 | pfleura2 | ! |
| 107 | 1 | pfleura2 | If (XDONT(IMIL) < XDONT(IDEB)) Then |
| 108 | 1 | pfleura2 | XWRK = XDONT (IDEB) |
| 109 | 1 | pfleura2 | XDONT (IDEB) = XDONT (IMIL) |
| 110 | 1 | pfleura2 | XDONT (IMIL) = XWRK |
| 111 | 1 | pfleura2 | End If |
| 112 | 1 | pfleura2 | If (XDONT(IMIL) > XDONT(IFIN)) Then |
| 113 | 1 | pfleura2 | XWRK = XDONT (IFIN) |
| 114 | 1 | pfleura2 | XDONT (IFIN) = XDONT (IMIL) |
| 115 | 1 | pfleura2 | XDONT (IMIL) = XWRK |
| 116 | 1 | pfleura2 | If (XDONT(IMIL) < XDONT(IDEB)) Then |
| 117 | 1 | pfleura2 | XWRK = XDONT (IDEB) |
| 118 | 1 | pfleura2 | XDONT (IDEB) = XDONT (IMIL) |
| 119 | 1 | pfleura2 | XDONT (IMIL) = XWRK |
| 120 | 1 | pfleura2 | End If |
| 121 | 1 | pfleura2 | End If |
| 122 | 1 | pfleura2 | XPIV = XDONT (IMIL) |
| 123 | 1 | pfleura2 | ! |
| 124 | 1 | pfleura2 | ! One exchanges values to put those > pivot in the end and |
| 125 | 1 | pfleura2 | ! those <= pivot at the beginning |
| 126 | 1 | pfleura2 | ! |
| 127 | 1 | pfleura2 | ICRS = IDEB |
| 128 | 1 | pfleura2 | IDCR = IFIN |
| 129 | 1 | pfleura2 | ECH2: Do |
| 130 | 1 | pfleura2 | Do |
| 131 | 1 | pfleura2 | ICRS = ICRS + 1 |
| 132 | 1 | pfleura2 | If (ICRS >= IDCR) Then |
| 133 | 1 | pfleura2 | ! |
| 134 | 1 | pfleura2 | ! the first > pivot is IDCR |
| 135 | 1 | pfleura2 | ! the last <= pivot is ICRS-1 |
| 136 | 1 | pfleura2 | ! Note: If one arrives here on the first iteration, then |
| 137 | 1 | pfleura2 | ! the pivot is the maximum of the set, the last value is equal |
| 138 | 1 | pfleura2 | ! to it, and one can reduce by one the size of the set to process, |
| 139 | 1 | pfleura2 | ! as if XDONT (IFIN) > XPIV |
| 140 | 1 | pfleura2 | ! |
| 141 | 1 | pfleura2 | Exit ECH2 |
| 142 | 1 | pfleura2 | ! |
| 143 | 1 | pfleura2 | End If |
| 144 | 1 | pfleura2 | If (XDONT(ICRS) > XPIV) Exit |
| 145 | 1 | pfleura2 | End Do |
| 146 | 1 | pfleura2 | Do |
| 147 | 1 | pfleura2 | If (XDONT(IDCR) <= XPIV) Exit |
| 148 | 1 | pfleura2 | IDCR = IDCR - 1 |
| 149 | 1 | pfleura2 | If (ICRS >= IDCR) Then |
| 150 | 1 | pfleura2 | ! |
| 151 | 1 | pfleura2 | ! The last value < pivot is always ICRS-1 |
| 152 | 1 | pfleura2 | ! |
| 153 | 1 | pfleura2 | Exit ECH2 |
| 154 | 1 | pfleura2 | End If |
| 155 | 1 | pfleura2 | End Do |
| 156 | 1 | pfleura2 | ! |
| 157 | 1 | pfleura2 | XWRK = XDONT (IDCR) |
| 158 | 1 | pfleura2 | XDONT (IDCR) = XDONT (ICRS) |
| 159 | 1 | pfleura2 | XDONT (ICRS) = XWRK |
| 160 | 1 | pfleura2 | End Do ECH2 |
| 161 | 1 | pfleura2 | ! |
| 162 | 1 | pfleura2 | ! One now sorts each of the two sub-intervals |
| 163 | 1 | pfleura2 | ! |
| 164 | 1 | pfleura2 | Call D_subsor (XDONT, IDEB1, ICRS-1) |
| 165 | 1 | pfleura2 | Call D_subsor (XDONT, IDCR, IFIN1) |
| 166 | 1 | pfleura2 | End If |
| 167 | 1 | pfleura2 | Return |
| 168 | 1 | pfleura2 | End Subroutine D_subsor |
| 169 | 1 | pfleura2 | |
| 170 | 1 | pfleura2 | Subroutine D_inssor (XDONT) |
| 171 | 1 | pfleura2 | ! Sorts XDONT into increasing order (Insertion sort) |
| 172 | 1 | pfleura2 | ! __________________________________________________________ |
| 173 | 1 | pfleura2 | |
| 174 | 1 | pfleura2 | use VarTypes |
| 175 | 1 | pfleura2 | |
| 176 | 1 | pfleura2 | IMPLICIT NONE |
| 177 | 1 | pfleura2 | Real(KREAL), dimension (:), Intent (InOut) :: XDONT |
| 178 | 1 | pfleura2 | ! __________________________________________________________ |
| 179 | 1 | pfleura2 | Integer(KINT) :: ICRS, IDCR |
| 180 | 1 | pfleura2 | Real(KREAL) :: XWRK |
| 181 | 1 | pfleura2 | ! |
| 182 | 1 | pfleura2 | Do ICRS = 2, Size (XDONT) |
| 183 | 1 | pfleura2 | XWRK = XDONT (ICRS) |
| 184 | 1 | pfleura2 | If (XWRK >= XDONT(ICRS-1)) Cycle |
| 185 | 1 | pfleura2 | XDONT (ICRS) = XDONT (ICRS-1) |
| 186 | 1 | pfleura2 | Do IDCR = ICRS - 2, 1, - 1 |
| 187 | 1 | pfleura2 | If (XWRK >= XDONT(IDCR)) Exit |
| 188 | 1 | pfleura2 | XDONT (IDCR+1) = XDONT (IDCR) |
| 189 | 1 | pfleura2 | End Do |
| 190 | 1 | pfleura2 | XDONT (IDCR+1) = XWRK |
| 191 | 1 | pfleura2 | End Do |
| 192 | 1 | pfleura2 | ! |
| 193 | 1 | pfleura2 | Return |
| 194 | 1 | pfleura2 | ! |
| 195 | 1 | pfleura2 | End Subroutine D_inssor |
| 196 | 1 | pfleura2 | |
| 197 | 1 | pfleura2 | |
| 198 | 1 | pfleura2 | ! ! |
| 199 | 1 | pfleura2 | ! Subroutine R_refsor (XDONT) |
| 200 | 1 | pfleura2 | ! ! Sorts XDONT into ascending order - Quicksort |
| 201 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 202 | 1 | pfleura2 | ! ! Quicksort chooses a "pivot" in the set, and explores the |
| 203 | 1 | pfleura2 | ! ! array from both ends, looking for a value > pivot with the |
| 204 | 1 | pfleura2 | ! ! increasing index, for a value <= pivot with the decreasing |
| 205 | 1 | pfleura2 | ! ! index, and swapping them when it has found one of each. |
| 206 | 1 | pfleura2 | ! ! The array is then subdivided in 2 ([3]) subsets: |
| 207 | 1 | pfleura2 | ! ! { values <= pivot} {pivot} {values > pivot}
|
| 208 | 1 | pfleura2 | ! ! One then call recursively the program to sort each subset. |
| 209 | 1 | pfleura2 | ! ! When the size of the subarray is small enough, one uses an |
| 210 | 1 | pfleura2 | ! ! insertion sort that is faster for very small sets. |
| 211 | 1 | pfleura2 | ! ! Michel Olagnon - Apr. 2000 |
| 212 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 213 | 1 | pfleura2 | ! ! _________________________________________________________ |
| 214 | 1 | pfleura2 | ! Real, Dimension (:), Intent (InOut) :: XDONT |
| 215 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 216 | 1 | pfleura2 | ! ! |
| 217 | 1 | pfleura2 | ! ! |
| 218 | 1 | pfleura2 | ! Call R_subsor (XDONT, 1, Size (XDONT)) |
| 219 | 1 | pfleura2 | ! Call R_inssor (XDONT) |
| 220 | 1 | pfleura2 | ! Return |
| 221 | 1 | pfleura2 | ! End Subroutine R_refsor |
| 222 | 1 | pfleura2 | |
| 223 | 1 | pfleura2 | ! Recursive Subroutine R_subsor (XDONT, IDEB1, IFIN1) |
| 224 | 1 | pfleura2 | ! ! Sorts XDONT from IDEB1 to IFIN1 |
| 225 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 226 | 1 | pfleura2 | ! Real, dimension (:), Intent (InOut) :: XDONT |
| 227 | 1 | pfleura2 | ! Integer, Intent (In) :: IDEB1, IFIN1 |
| 228 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 229 | 1 | pfleura2 | ! Integer, Parameter :: NINS = 16 ! Max for insertion sort |
| 230 | 1 | pfleura2 | ! Integer :: ICRS, IDEB, IDCR, IFIN, IMIL |
| 231 | 1 | pfleura2 | ! Real :: XPIV, XWRK |
| 232 | 1 | pfleura2 | ! ! |
| 233 | 1 | pfleura2 | ! IDEB = IDEB1 |
| 234 | 1 | pfleura2 | ! IFIN = IFIN1 |
| 235 | 1 | pfleura2 | ! ! |
| 236 | 1 | pfleura2 | ! ! If we don't have enough values to make it worth while, we leave |
| 237 | 1 | pfleura2 | ! ! them unsorted, and the final insertion sort will take care of them |
| 238 | 1 | pfleura2 | ! ! |
| 239 | 1 | pfleura2 | ! If ((IFIN - IDEB) > NINS) Then |
| 240 | 1 | pfleura2 | ! IMIL = (IDEB+IFIN) / 2 |
| 241 | 1 | pfleura2 | ! ! |
| 242 | 1 | pfleura2 | ! ! One chooses a pivot, median of 1st, last, and middle values |
| 243 | 1 | pfleura2 | ! ! |
| 244 | 1 | pfleura2 | ! If (XDONT(IMIL) < XDONT(IDEB)) Then |
| 245 | 1 | pfleura2 | ! XWRK = XDONT (IDEB) |
| 246 | 1 | pfleura2 | ! XDONT (IDEB) = XDONT (IMIL) |
| 247 | 1 | pfleura2 | ! XDONT (IMIL) = XWRK |
| 248 | 1 | pfleura2 | ! End If |
| 249 | 1 | pfleura2 | ! If (XDONT(IMIL) > XDONT(IFIN)) Then |
| 250 | 1 | pfleura2 | ! XWRK = XDONT (IFIN) |
| 251 | 1 | pfleura2 | ! XDONT (IFIN) = XDONT (IMIL) |
| 252 | 1 | pfleura2 | ! XDONT (IMIL) = XWRK |
| 253 | 1 | pfleura2 | ! If (XDONT(IMIL) < XDONT(IDEB)) Then |
| 254 | 1 | pfleura2 | ! XWRK = XDONT (IDEB) |
| 255 | 1 | pfleura2 | ! XDONT (IDEB) = XDONT (IMIL) |
| 256 | 1 | pfleura2 | ! XDONT (IMIL) = XWRK |
| 257 | 1 | pfleura2 | ! End If |
| 258 | 1 | pfleura2 | ! End If |
| 259 | 1 | pfleura2 | ! XPIV = XDONT (IMIL) |
| 260 | 1 | pfleura2 | ! ! |
| 261 | 1 | pfleura2 | ! ! One exchanges values to put those > pivot in the end and |
| 262 | 1 | pfleura2 | ! ! those <= pivot at the beginning |
| 263 | 1 | pfleura2 | ! ! |
| 264 | 1 | pfleura2 | ! ICRS = IDEB |
| 265 | 1 | pfleura2 | ! IDCR = IFIN |
| 266 | 1 | pfleura2 | ! ECH2: Do |
| 267 | 1 | pfleura2 | ! Do |
| 268 | 1 | pfleura2 | ! ICRS = ICRS + 1 |
| 269 | 1 | pfleura2 | ! If (ICRS >= IDCR) Then |
| 270 | 1 | pfleura2 | ! ! |
| 271 | 1 | pfleura2 | ! ! the first > pivot is IDCR |
| 272 | 1 | pfleura2 | ! ! the last <= pivot is ICRS-1 |
| 273 | 1 | pfleura2 | ! ! Note: If one arrives here on the first iteration, then |
| 274 | 1 | pfleura2 | ! ! the pivot is the maximum of the set, the last value is equal |
| 275 | 1 | pfleura2 | ! ! to it, and one can reduce by one the size of the set to process, |
| 276 | 1 | pfleura2 | ! ! as if XDONT (IFIN) > XPIV |
| 277 | 1 | pfleura2 | ! ! |
| 278 | 1 | pfleura2 | ! Exit ECH2 |
| 279 | 1 | pfleura2 | ! ! |
| 280 | 1 | pfleura2 | ! End If |
| 281 | 1 | pfleura2 | ! If (XDONT(ICRS) > XPIV) Exit |
| 282 | 1 | pfleura2 | ! End Do |
| 283 | 1 | pfleura2 | ! Do |
| 284 | 1 | pfleura2 | ! If (XDONT(IDCR) <= XPIV) Exit |
| 285 | 1 | pfleura2 | ! IDCR = IDCR - 1 |
| 286 | 1 | pfleura2 | ! If (ICRS >= IDCR) Then |
| 287 | 1 | pfleura2 | ! ! |
| 288 | 1 | pfleura2 | ! ! The last value < pivot is always ICRS-1 |
| 289 | 1 | pfleura2 | ! ! |
| 290 | 1 | pfleura2 | ! Exit ECH2 |
| 291 | 1 | pfleura2 | ! End If |
| 292 | 1 | pfleura2 | ! End Do |
| 293 | 1 | pfleura2 | ! ! |
| 294 | 1 | pfleura2 | ! XWRK = XDONT (IDCR) |
| 295 | 1 | pfleura2 | ! XDONT (IDCR) = XDONT (ICRS) |
| 296 | 1 | pfleura2 | ! XDONT (ICRS) = XWRK |
| 297 | 1 | pfleura2 | ! End Do ECH2 |
| 298 | 1 | pfleura2 | ! ! |
| 299 | 1 | pfleura2 | ! ! One now sorts each of the two sub-intervals |
| 300 | 1 | pfleura2 | ! ! |
| 301 | 1 | pfleura2 | ! Call R_subsor (XDONT, IDEB1, ICRS-1) |
| 302 | 1 | pfleura2 | ! Call R_subsor (XDONT, IDCR, IFIN1) |
| 303 | 1 | pfleura2 | ! End If |
| 304 | 1 | pfleura2 | ! Return |
| 305 | 1 | pfleura2 | ! End Subroutine R_subsor |
| 306 | 1 | pfleura2 | ! Subroutine R_inssor (XDONT) |
| 307 | 1 | pfleura2 | ! ! Sorts XDONT into increasing order (Insertion sort) |
| 308 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 309 | 1 | pfleura2 | ! Real, dimension (:), Intent (InOut) :: XDONT |
| 310 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 311 | 1 | pfleura2 | ! Integer :: ICRS, IDCR |
| 312 | 1 | pfleura2 | ! Real :: XWRK |
| 313 | 1 | pfleura2 | ! ! |
| 314 | 1 | pfleura2 | ! Do ICRS = 2, Size (XDONT) |
| 315 | 1 | pfleura2 | ! XWRK = XDONT (ICRS) |
| 316 | 1 | pfleura2 | ! If (XWRK >= XDONT(ICRS-1)) Cycle |
| 317 | 1 | pfleura2 | ! XDONT (ICRS) = XDONT (ICRS-1) |
| 318 | 1 | pfleura2 | ! Do IDCR = ICRS - 2, 1, - 1 |
| 319 | 1 | pfleura2 | ! If (XWRK >= XDONT(IDCR)) Exit |
| 320 | 1 | pfleura2 | ! XDONT (IDCR+1) = XDONT (IDCR) |
| 321 | 1 | pfleura2 | ! End Do |
| 322 | 1 | pfleura2 | ! XDONT (IDCR+1) = XWRK |
| 323 | 1 | pfleura2 | ! End Do |
| 324 | 1 | pfleura2 | ! ! |
| 325 | 1 | pfleura2 | ! Return |
| 326 | 1 | pfleura2 | ! ! |
| 327 | 1 | pfleura2 | ! End Subroutine R_inssor |
| 328 | 1 | pfleura2 | ! ! |
| 329 | 1 | pfleura2 | ! Subroutine I_refsor (XDONT) |
| 330 | 1 | pfleura2 | ! ! Sorts XDONT into ascending order - Quicksort |
| 331 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 332 | 1 | pfleura2 | ! ! Quicksort chooses a "pivot" in the set, and explores the |
| 333 | 1 | pfleura2 | ! ! array from both ends, looking for a value > pivot with the |
| 334 | 1 | pfleura2 | ! ! increasing index, for a value <= pivot with the decreasing |
| 335 | 1 | pfleura2 | ! ! index, and swapping them when it has found one of each. |
| 336 | 1 | pfleura2 | ! ! The array is then subdivided in 2 ([3]) subsets: |
| 337 | 1 | pfleura2 | ! ! { values <= pivot} {pivot} {values > pivot}
|
| 338 | 1 | pfleura2 | ! ! One then call recursively the program to sort each subset. |
| 339 | 1 | pfleura2 | ! ! When the size of the subarray is small enough, one uses an |
| 340 | 1 | pfleura2 | ! ! insertion sort that is faster for very small sets. |
| 341 | 1 | pfleura2 | ! ! Michel Olagnon - Apr. 2000 |
| 342 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 343 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 344 | 1 | pfleura2 | ! Integer, Dimension (:), Intent (InOut) :: XDONT |
| 345 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 346 | 1 | pfleura2 | ! ! |
| 347 | 1 | pfleura2 | ! ! |
| 348 | 1 | pfleura2 | ! Call I_subsor (XDONT, 1, Size (XDONT)) |
| 349 | 1 | pfleura2 | ! Call I_inssor (XDONT) |
| 350 | 1 | pfleura2 | ! Return |
| 351 | 1 | pfleura2 | ! End Subroutine I_refsor |
| 352 | 1 | pfleura2 | ! Recursive Subroutine I_subsor (XDONT, IDEB1, IFIN1) |
| 353 | 1 | pfleura2 | ! ! Sorts XDONT from IDEB1 to IFIN1 |
| 354 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 355 | 1 | pfleura2 | ! Integer, dimension (:), Intent (InOut) :: XDONT |
| 356 | 1 | pfleura2 | ! Integer, Intent (In) :: IDEB1, IFIN1 |
| 357 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 358 | 1 | pfleura2 | ! Integer, Parameter :: NINS = 16 ! Max for insertion sort |
| 359 | 1 | pfleura2 | ! Integer :: ICRS, IDEB, IDCR, IFIN, IMIL |
| 360 | 1 | pfleura2 | ! Integer :: XPIV, XWRK |
| 361 | 1 | pfleura2 | ! ! |
| 362 | 1 | pfleura2 | ! IDEB = IDEB1 |
| 363 | 1 | pfleura2 | ! IFIN = IFIN1 |
| 364 | 1 | pfleura2 | ! ! |
| 365 | 1 | pfleura2 | ! ! If we don't have enough values to make it worth while, we leave |
| 366 | 1 | pfleura2 | ! ! them unsorted, and the final insertion sort will take care of them |
| 367 | 1 | pfleura2 | ! ! |
| 368 | 1 | pfleura2 | ! If ((IFIN - IDEB) > NINS) Then |
| 369 | 1 | pfleura2 | ! IMIL = (IDEB+IFIN) / 2 |
| 370 | 1 | pfleura2 | ! ! |
| 371 | 1 | pfleura2 | ! ! One chooses a pivot, median of 1st, last, and middle values |
| 372 | 1 | pfleura2 | ! ! |
| 373 | 1 | pfleura2 | ! If (XDONT(IMIL) < XDONT(IDEB)) Then |
| 374 | 1 | pfleura2 | ! XWRK = XDONT (IDEB) |
| 375 | 1 | pfleura2 | ! XDONT (IDEB) = XDONT (IMIL) |
| 376 | 1 | pfleura2 | ! XDONT (IMIL) = XWRK |
| 377 | 1 | pfleura2 | ! End If |
| 378 | 1 | pfleura2 | ! If (XDONT(IMIL) > XDONT(IFIN)) Then |
| 379 | 1 | pfleura2 | ! XWRK = XDONT (IFIN) |
| 380 | 1 | pfleura2 | ! XDONT (IFIN) = XDONT (IMIL) |
| 381 | 1 | pfleura2 | ! XDONT (IMIL) = XWRK |
| 382 | 1 | pfleura2 | ! If (XDONT(IMIL) < XDONT(IDEB)) Then |
| 383 | 1 | pfleura2 | ! XWRK = XDONT (IDEB) |
| 384 | 1 | pfleura2 | ! XDONT (IDEB) = XDONT (IMIL) |
| 385 | 1 | pfleura2 | ! XDONT (IMIL) = XWRK |
| 386 | 1 | pfleura2 | ! End If |
| 387 | 1 | pfleura2 | ! End If |
| 388 | 1 | pfleura2 | ! XPIV = XDONT (IMIL) |
| 389 | 1 | pfleura2 | ! ! |
| 390 | 1 | pfleura2 | ! ! One exchanges values to put those > pivot in the end and |
| 391 | 1 | pfleura2 | ! ! those <= pivot at the beginning |
| 392 | 1 | pfleura2 | ! ! |
| 393 | 1 | pfleura2 | ! ICRS = IDEB |
| 394 | 1 | pfleura2 | ! IDCR = IFIN |
| 395 | 1 | pfleura2 | ! ECH2: Do |
| 396 | 1 | pfleura2 | ! Do |
| 397 | 1 | pfleura2 | ! ICRS = ICRS + 1 |
| 398 | 1 | pfleura2 | ! If (ICRS >= IDCR) Then |
| 399 | 1 | pfleura2 | ! ! |
| 400 | 1 | pfleura2 | ! ! the first > pivot is IDCR |
| 401 | 1 | pfleura2 | ! ! the last <= pivot is ICRS-1 |
| 402 | 1 | pfleura2 | ! ! Note: If one arrives here on the first iteration, then |
| 403 | 1 | pfleura2 | ! ! the pivot is the maximum of the set, the last value is equal |
| 404 | 1 | pfleura2 | ! ! to it, and one can reduce by one the size of the set to process, |
| 405 | 1 | pfleura2 | ! ! as if XDONT (IFIN) > XPIV |
| 406 | 1 | pfleura2 | ! ! |
| 407 | 1 | pfleura2 | ! Exit ECH2 |
| 408 | 1 | pfleura2 | ! ! |
| 409 | 1 | pfleura2 | ! End If |
| 410 | 1 | pfleura2 | ! If (XDONT(ICRS) > XPIV) Exit |
| 411 | 1 | pfleura2 | ! End Do |
| 412 | 1 | pfleura2 | ! Do |
| 413 | 1 | pfleura2 | ! If (XDONT(IDCR) <= XPIV) Exit |
| 414 | 1 | pfleura2 | ! IDCR = IDCR - 1 |
| 415 | 1 | pfleura2 | ! If (ICRS >= IDCR) Then |
| 416 | 1 | pfleura2 | ! ! |
| 417 | 1 | pfleura2 | ! ! The last value < pivot is always ICRS-1 |
| 418 | 1 | pfleura2 | ! ! |
| 419 | 1 | pfleura2 | ! Exit ECH2 |
| 420 | 1 | pfleura2 | ! End If |
| 421 | 1 | pfleura2 | ! End Do |
| 422 | 1 | pfleura2 | ! ! |
| 423 | 1 | pfleura2 | ! XWRK = XDONT (IDCR) |
| 424 | 1 | pfleura2 | ! XDONT (IDCR) = XDONT (ICRS) |
| 425 | 1 | pfleura2 | ! XDONT (ICRS) = XWRK |
| 426 | 1 | pfleura2 | ! End Do ECH2 |
| 427 | 1 | pfleura2 | ! ! |
| 428 | 1 | pfleura2 | ! ! One now sorts each of the two sub-intervals |
| 429 | 1 | pfleura2 | ! ! |
| 430 | 1 | pfleura2 | ! Call I_subsor (XDONT, IDEB1, ICRS-1) |
| 431 | 1 | pfleura2 | ! Call I_subsor (XDONT, IDCR, IFIN1) |
| 432 | 1 | pfleura2 | ! End If |
| 433 | 1 | pfleura2 | ! Return |
| 434 | 1 | pfleura2 | ! End Subroutine I_subsor |
| 435 | 1 | pfleura2 | ! Subroutine I_inssor (XDONT) |
| 436 | 1 | pfleura2 | ! ! Sorts XDONT into increasing order (Insertion sort) |
| 437 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 438 | 1 | pfleura2 | ! Integer, dimension (:), Intent (InOut) :: XDONT |
| 439 | 1 | pfleura2 | ! ! __________________________________________________________ |
| 440 | 1 | pfleura2 | ! Integer :: ICRS, IDCR |
| 441 | 1 | pfleura2 | ! Integer :: XWRK |
| 442 | 1 | pfleura2 | ! ! |
| 443 | 1 | pfleura2 | ! Do ICRS = 2, Size (XDONT) |
| 444 | 1 | pfleura2 | ! XWRK = XDONT (ICRS) |
| 445 | 1 | pfleura2 | ! If (XWRK >= XDONT(ICRS-1)) Cycle |
| 446 | 1 | pfleura2 | ! XDONT (ICRS) = XDONT (ICRS-1) |
| 447 | 1 | pfleura2 | ! Do IDCR = ICRS - 2, 1, - 1 |
| 448 | 1 | pfleura2 | ! If (XWRK >= XDONT(IDCR)) Exit |
| 449 | 1 | pfleura2 | ! XDONT (IDCR+1) = XDONT (IDCR) |
| 450 | 1 | pfleura2 | ! End Do |
| 451 | 1 | pfleura2 | ! XDONT (IDCR+1) = XWRK |
| 452 | 1 | pfleura2 | ! End Do |
| 453 | 1 | pfleura2 | ! ! |
| 454 | 1 | pfleura2 | ! Return |
| 455 | 1 | pfleura2 | ! ! |
| 456 | 1 | pfleura2 | ! End Subroutine I_inssor |
| 457 | 1 | pfleura2 | ! ! |
| 458 | 1 | pfleura2 | ! end module m_refsor |