153 integer(I4B),
dimension(:),
intent(inout) :: indx
154 real(DP),
dimension(:),
intent(inout) :: v
155 logical,
intent(in),
optional :: reverse
158 integer(I4B),
parameter :: nn = 15
159 integer(I4B),
parameter :: nstack = 50
160 integer(I4B) :: nsize
164 integer(I4B) :: jstack
165 integer(I4B) :: ileft
166 integer(I4B) :: iright
167 integer(I4B),
dimension(nstack) :: istack
175 if (
present(reverse))
then
189 if (iright - ileft < nn)
then
190 do j = (ileft + 1), iright
193 do i = (j - 1), ileft, -1
196 indx(i + 1) = indx(i)
201 if (jstack == 0)
return
202 iright = istack(jstack)
203 ileft = istack(jstack - 1)
206 k = (ileft + iright) / 2
207 call rswap(v(k), v(ileft + 1))
208 call iswap(indx(k), indx(ileft + 1))
209 if (v(ileft) > v(iright))
then
210 call rswap(v(ileft), v(iright))
211 call iswap(indx(ileft), indx(iright))
213 if (v(ileft + 1) > v(iright))
then
214 call rswap(v(ileft + 1), v(iright))
215 call iswap(indx(ileft + 1), indx(iright))
217 if (v(ileft) > v(ileft + 1))
then
218 call rswap(v(ileft), v(ileft + 1))
219 call iswap(indx(ileft), indx(ileft + 1))
241 call rswap(v(i), v(j))
242 call iswap(indx(i), indx(j))
245 indx(ileft + 1) = indx(j)
249 if (jstack > nstack)
then
250 write (errmsg,
'(a,3(1x,a))') &
251 'JSTACK > NSTACK IN SortModule::qsort'
252 call store_error(errmsg, terminate=.true.)
254 if ((iright - i + 1) >= (j - 1))
then
255 istack(jstack) = iright
256 istack(jstack - 1) = i
259 istack(jstack) = j - 1
260 istack(jstack - 1) = ileft
270 call rswap(v(i), v(j))
271 call iswap(indx(i), indx(j))