| 1 |
!----------------------------------------------------------------- |
| 2 |
! |
| 3 |
! This file is (or was) part of SPLASH, a visualisation tool |
| 4 |
! for Smoothed Particle Hydrodynamics written by Daniel Price: |
| 5 |
! |
| 6 |
! http://users.monash.edu.au/~dprice/splash |
| 7 |
! |
| 8 |
! SPLASH comes with ABSOLUTELY NO WARRANTY. |
| 9 |
! This is free software; and you are welcome to redistribute |
| 10 |
! it under the terms of the GNU General Public License |
| 11 |
! (see LICENSE file for details) and the provision that |
| 12 |
! this notice remains intact. If you modify this file, please |
| 13 |
! note section 2a) of the GPLv2 states that: |
| 14 |
! |
| 15 |
! a) You must cause the modified files to carry prominent notices |
| 16 |
! stating that you changed the files and the date of any change. |
| 17 |
! |
| 18 |
! Copyright (C) 2005-2017 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
module sort |
| 23 |
implicit none |
| 24 |
public :: indexx, indexxi |
| 25 |
private |
| 26 |
|
| 27 |
contains |
| 28 |
|
| 29 |
subroutine indexx(n, arr, indx) |
| 30 |
!************************************************************ |
| 31 |
! * |
| 32 |
! This is INDEXX using the quicksort algorithm. * |
| 33 |
! * |
| 34 |
!************************************************************ |
| 35 |
integer, parameter :: m=7, nstack=500 |
| 36 |
integer, intent(in) :: n |
| 37 |
real, dimension(n), intent(in) :: arr |
| 38 |
integer, dimension(n), intent(out) :: indx |
| 39 |
|
| 40 |
integer :: i,j,k,l,ir,jstack,indxt,itemp |
| 41 |
integer, dimension(nstack) :: istack |
| 42 |
real :: a |
| 43 |
|
| 44 |
do j = 1, n |
| 45 |
indx(j) = j |
| 46 |
enddo |
| 47 |
jstack = 0 |
| 48 |
l = 1 |
| 49 |
ir = n |
| 50 |
|
| 51 |
1 if (ir - l < m) then |
| 52 |
do j = l + 1, ir |
| 53 |
indxt = indx(j) |
| 54 |
a = arr(indxt) |
| 55 |
do i = j - 1, 1, -1 |
| 56 |
if (arr(indx(i)) <= a) goto 2 |
| 57 |
indx(i + 1) = indx(i) |
| 58 |
enddo |
| 59 |
i = 0 |
| 60 |
2 indx(i + 1) = indxt |
| 61 |
enddo |
| 62 |
if (jstack==0) return |
| 63 |
ir = istack(jstack) |
| 64 |
l = istack(jstack - 1) |
| 65 |
jstack = jstack - 2 |
| 66 |
else |
| 67 |
k = (l + ir)/2 |
| 68 |
itemp = indx(k) |
| 69 |
indx(k) = indx(l + 1) |
| 70 |
indx(l + 1) = itemp |
| 71 |
if (arr(indx(l + 1)) > arr(indx(ir))) then |
| 72 |
itemp = indx(l + 1) |
| 73 |
indx(l + 1) = indx(ir) |
| 74 |
indx(ir) = itemp |
| 75 |
endif |
| 76 |
if (arr(indx(l)) > arr(indx(ir))) then |
| 77 |
itemp = indx(l) |
| 78 |
indx(l) = indx(ir) |
| 79 |
indx(ir) = itemp |
| 80 |
endif |
| 81 |
if (arr(indx(l + 1)) > arr(indx(l))) then |
| 82 |
itemp = indx(l + 1) |
| 83 |
indx(l + 1) = indx(l) |
| 84 |
indx(l) = itemp |
| 85 |
endif |
| 86 |
i = l + 1 |
| 87 |
j = ir |
| 88 |
indxt = indx(l) |
| 89 |
a = arr(indxt) |
| 90 |
3 continue |
| 91 |
i = i + 1 |
| 92 |
if (arr(indx(i)) < a) goto 3 |
| 93 |
4 continue |
| 94 |
j = j - 1 |
| 95 |
if (arr(indx(j)) > a) goto 4 |
| 96 |
if (j < i) goto 5 |
| 97 |
itemp = indx(i) |
| 98 |
indx(i) = indx(j) |
| 99 |
indx(j) = itemp |
| 100 |
goto 3 |
| 101 |
|
| 102 |
5 indx(l) = indx(j) |
| 103 |
indx(j) = indxt |
| 104 |
jstack = jstack + 2 |
| 105 |
if (jstack > nstack) then |
| 106 |
print*,'fatal error!!! stacksize exceeded in sort' |
| 107 |
print*,'(need to set parameter nstack higher in subroutine indexx ' |
| 108 |
print*,' this is in the file sort.f90)' |
| 109 |
stop |
| 110 |
endif |
| 111 |
if (ir - i + 1 >= j - l) then |
| 112 |
istack(jstack) = ir |
| 113 |
istack(jstack - 1) = i |
| 114 |
ir = j - 1 |
| 115 |
else |
| 116 |
istack(jstack) = j - 1 |
| 117 |
istack(jstack - 1) = l |
| 118 |
l = i |
| 119 |
endif |
| 120 |
endif |
| 121 |
|
| 122 |
goto 1 |
| 123 |
end subroutine indexx |
| 124 |
|
| 125 |
!---------------------------------- |
| 126 |
! integer version of above routine |
| 127 |
!---------------------------------- |
| 128 |
subroutine indexxi(n, arr, indx) |
| 129 |
!************************************************************ |
| 130 |
! * |
| 131 |
! This is INDEXX using the quicksort algorithm. * |
| 132 |
! * |
| 133 |
!************************************************************ |
| 134 |
integer, parameter :: m=7, nstack=500 |
| 135 |
integer, intent(in) :: n |
| 136 |
integer, dimension(n), intent(in) :: arr |
| 137 |
integer, dimension(n), intent(out) :: indx |
| 138 |
|
| 139 |
integer :: i,j,k,l,ir,jstack,indxt,itemp |
| 140 |
integer, dimension(nstack) :: istack |
| 141 |
integer :: a |
| 142 |
|
| 143 |
do j = 1, n |
| 144 |
indx(j) = j |
| 145 |
enddo |
| 146 |
jstack = 0 |
| 147 |
l = 1 |
| 148 |
ir = n |
| 149 |
|
| 150 |
1 if (ir - l < m) then |
| 151 |
do j = l + 1, ir |
| 152 |
indxt = indx(j) |
| 153 |
a = arr(indxt) |
| 154 |
do i = j - 1, 1, -1 |
| 155 |
if (arr(indx(i)) <= a) goto 2 |
| 156 |
indx(i + 1) = indx(i) |
| 157 |
enddo |
| 158 |
i = 0 |
| 159 |
2 indx(i + 1) = indxt |
| 160 |
enddo |
| 161 |
if (jstack==0) return |
| 162 |
ir = istack(jstack) |
| 163 |
l = istack(jstack - 1) |
| 164 |
jstack = jstack - 2 |
| 165 |
else |
| 166 |
k = (l + ir)/2 |
| 167 |
itemp = indx(k) |
| 168 |
indx(k) = indx(l + 1) |
| 169 |
indx(l + 1) = itemp |
| 170 |
if (arr(indx(l + 1)) > arr(indx(ir))) then |
| 171 |
itemp = indx(l + 1) |
| 172 |
indx(l + 1) = indx(ir) |
| 173 |
indx(ir) = itemp |
| 174 |
endif |
| 175 |
if (arr(indx(l)) > arr(indx(ir))) then |
| 176 |
itemp = indx(l) |
| 177 |
indx(l) = indx(ir) |
| 178 |
indx(ir) = itemp |
| 179 |
endif |
| 180 |
if (arr(indx(l + 1)) > arr(indx(l))) then |
| 181 |
itemp = indx(l + 1) |
| 182 |
indx(l + 1) = indx(l) |
| 183 |
indx(l) = itemp |
| 184 |
endif |
| 185 |
i = l + 1 |
| 186 |
j = ir |
| 187 |
indxt = indx(l) |
| 188 |
a = arr(indxt) |
| 189 |
3 continue |
| 190 |
i = i + 1 |
| 191 |
if (arr(indx(i)) < a) goto 3 |
| 192 |
4 continue |
| 193 |
j = j - 1 |
| 194 |
if (arr(indx(j)) > a) goto 4 |
| 195 |
if (j < i) goto 5 |
| 196 |
itemp = indx(i) |
| 197 |
indx(i) = indx(j) |
| 198 |
indx(j) = itemp |
| 199 |
goto 3 |
| 200 |
|
| 201 |
5 indx(l) = indx(j) |
| 202 |
indx(j) = indxt |
| 203 |
jstack = jstack + 2 |
| 204 |
if (jstack > nstack) then |
| 205 |
print*,'fatal error!!! stacksize exceeded in sort' |
| 206 |
print*,'(need to set parameter nstack higher in subroutine indexx ' |
| 207 |
print*,' this is in the file sort.f90)' |
| 208 |
stop |
| 209 |
endif |
| 210 |
if (ir - i + 1 >= j - l) then |
| 211 |
istack(jstack) = ir |
| 212 |
istack(jstack - 1) = i |
| 213 |
ir = j - 1 |
| 214 |
else |
| 215 |
istack(jstack) = j - 1 |
| 216 |
istack(jstack - 1) = l |
| 217 |
l = i |
| 218 |
endif |
| 219 |
endif |
| 220 |
|
| 221 |
goto 1 |
| 222 |
end subroutine indexxi |
| 223 |
|
| 224 |
end module sort |