COSMOS v7.655  COSMOSv7655
(AirShowerMC)
kqsort.f
Go to the documentation of this file.
1 !
2 ! call kqsort(compf, idx, n)
3 !
4 ! kqsort is a quick sort routine for any data type in a 1 dimensional
5 ! array. (data type may be real, integer, character, double precision
6 ! real or integer, half integer)
7 ! It can sort the data in ascending or descending order.
8 ! If you follow the instructions below, you will get ascending sort.
9 !
10 ! To use kqsort, you have to supply a simple integer function for each
11 ! array you want to sort. The name is arbitrary, and must be like
12 !
13 ! integer compf
14 ! external compf
15 ! --------------------
16 ! integer n
17 ! parameter (n = 10000)
18 ! real x(n)
19 ! common /abc/ x
20 ! -------------------
21 ! integer idx(n)
22 !
23 ! x(*) is computed somewhere
24 ! call kqsort(compf, idx, n)
25 ! Then, idx gets sorted order as (in default)
26 ! x(idx(1)) <= x(idx(2)) <= x(idx(3)).... <= x(idx(n))
27 !
28 ! ,,,
29 ! integer function compf(i, j)
30 ! integer i, j
31 ! --------------------
32 ! integer n
33 ! parameter (n = 10000)
34 ! real x(n)
35 ! common /abc/ x
36 ! -------------------
37 ! if( x(i) .lt. x(j)) then
38 ! compf = -1 ! put 1 if you want descending sort
39 ! elseif( x(i) .gt. x(j)) then
40 ! compf = 1 ! put -1 if you want descending sort
41 ! else
42 ! compf = 0
43 ! endif
44 ! end
45 ! *******************************************
46 ! If you want to have descending order, you may use
47 ! idx(n) to idx(1). However, this may lead to some confusion,
48 ! so you can get descening sort directly. There are two method:
49 !
50 ! 1) After calling kqsort in a default manner, you may call
51 ! call ksortinv(idx, n)
52 ! Then, x(idx(1)) >= x(idx(2)) ... >= x(idx(n))
53 ! 2) You may construct compf integer funtcion as shown by the
54 ! comment in the above example (reverse the sing of the function
55 ! value).
56 !
57 ! You may worry about the overhead of calling ksortinv,
58 ! but the time for it can be negligiblly small as compared with
59 ! kqsort itself.
60 !
61 ! If you sort a large array (say, size >10^6)
62 ! many times, it may be better to use
63 ! kqsortd, kqsortr, kqsoti, kqsorth or kqsortc depending on
64 ! double precision real, real, integer, half integer, character
65 ! data. These routines don't require additonal integer function
66 ! like compf.
67 ! call kqsort?(x, idx, n)
68 ! The routies are for ascending order sort; if you
69 ! want descending sort, use ksortinv.
70 ! ********************
71 !
72 !cc test kqsort
73 ! implicit none
74 ! integer i, j
75 !
76 ! external dcompf, icompf, rcompf, ccompf
77 ! integer dcompf, icomf, rcompf, ccompf
78 ! real*8 u
79 !
80 ! integer n
81 ! parameter (n = 1000000)
82 ! real*8 a( n )
83 ! real b( n )
84 ! integer c( n )
85 ! character*9 x( 10 )
86 !
87 ! common /zzz/ a, b, c
88 ! common /zzzc/ x
89 !
90 ! integer idx(n)
91 !
92 ! do i = 1, n
93 ! call rndc(u)
94 ! b(i) = u
95 ! enddo
96 ! call kqsort(rcompf, idx, n)
97 ! do i = 1, n/2
98 ! j =idx(i)
99 ! idx(i) = idx(n-i+1)
100 ! idx(n-i+1) = j
101 ! enddo
102 !
103 ! do i=1, 10
104 ! write(*,*) i, b(idx(i))
105 ! enddo
106 ! do i=n-10, n
107 ! write(*,*) i, b(idx(i))
108 ! enddo
109 !
110 !
111 ! end
112 !
113 !
114 ! integer function dcompf(i, j)
115 ! integer i, j
116 ! integer n
117 ! parameter (n = 1000000)
118 ! real*8 a( n )
119 ! real b( n )
120 ! integer c( n )
121 ! character*9 x( 10 )
122 ! common /zzz/ a, b, c
123 ! common /zzzc/ x
124 !
125 !
126 ! if(a(i) .lt. a(j)) then
127 ! dcompf = -1
128 ! elseif(a(i) .eq. a(j)) then
129 ! dcompf = 0
130 ! else
131 ! dcompf = 1
132 ! endif
133 ! end
134 ! integer function rcompf(i, j)
135 ! integer i, j
136 ! integer n
137 ! parameter (n = 1000000)
138 ! real*8 a( n )
139 ! real b( n )
140 ! integer c( n )
141 ! character*9 x( 10 )
142 ! common /zzz/ a, b, c
143 ! common /zzzc/ x
144 !
145 !
146 ! if(b(i) .lt. b(j)) then
147 ! rcompf = -1
148 ! elseif(b(i) .eq. b(j)) then
149 ! rcompf = 0
150 ! else
151 ! rcompf = 1
152 ! endif
153 ! end
154 !
155 ! integer function icompf(i, j)
156 ! integer i, j
157 ! integer n
158 ! parameter (n = 1000000)
159 ! real*8 a( n )
160 ! real b( n )
161 ! integer c( n )
162 ! character*9 x( 10 )
163 ! common /zzz/ a, b, c
164 ! common /zzzc/ x
165 !
166 !
167 ! if(c(i) .lt. c(j)) then
168 ! icompf = -1
169 ! elseif(c(i) .eq. c(j)) then
170 ! icompf = 0
171 ! else
172 ! icompf = 1
173 ! endif
174 ! end
175 !
176 ! integer function ccompf(i, j)
177 ! integer i, j
178 ! integer n
179 ! parameter (n = 1000000)
180 ! real*8 a( n )
181 ! real b( n )
182 ! integer c( n )
183 ! character*9 x( 10 )
184 ! common /zzz/ a, b, c
185 ! common /zzzc/ x
186 !
187 !
188 ! if(x(i) .lt. x(j)) then
189 ! ccompf = -1
190 ! elseif(x(i) .eq. x(j)) then
191 ! ccompf = 0
192 ! else
193 ! ccompf = 1
194 ! endif
195 ! end
196 !
197  SUBROUTINE kqsort(compf, ORD,N)
198 !
199 !==============SORTS THE ARRAY A(I),I=1,2,...,N BY PUTTING THE
200 ! ASCENDING ORDER VECTOR IN ORD. THAT IS ASCENDING ORDERED A
201 ! IS A(ORD(I)),I=1,2,...,N; DESCENDING ORDER A IS A(ORD(N-I+1)),
202 ! I=1,2,...,N . THIS SORT RUNS IN TIME PROPORTIONAL TO N LOG N .
203 !
204 !
205 ! ACM QUICKSORT - ALGORITHM #402 - IMPLEMENTED IN FORTRAN BY
206 ! WILLIAM H. VERITY
207 ! COMPUTATION CENTER
208 ! PENNSYLVANIA STATE UNIVERSITY
209 ! UNIVERSITY PARK, PA. 16802
210 !
211  IMPLICIT INTEGER (A-Z)
212 !
213  dimension ord(n),poplst(2,20)
214  external compf
215 !c INTEGER X,XX,Z,ZZ,Y
216 !c CHARACTER *(*) A(N)
217 !
218  ndeep=0
219  u1=n
220  l1=1
221  DO 1 i=1,n
222  1 ord(i)=i
223  2 IF (u1.GT.l1) GO TO 3
224  RETURN
225 !
226  3 l=l1
227  u=u1
228 !
229 ! PART
230 !
231  4 p=l
232  q=u
233  x=ord(p)
234  z=ord(q)
235 
236 ! IF (A(X).LE.A(Z)) GO TO 5
237  if( compf(x, z) .le. 0 ) goto 5
238  y=x
239  x=z
240  z=y
241  yp=ord(p)
242  ord(p)=ord(q)
243  ord(q)=yp
244  5 IF (u-l.LE.1) GO TO 15
245  xx=x
246  ix=p
247  zz=z
248  iz=q
249 !
250 ! LEFT
251 !
252  6 p=p+1
253  IF (p.GE.q) GO TO 7
254  x=ord(p)
255 ! IF (A(X).GE.A(XX)) GO TO 8
256  if( compf(x, xx) .ge. 0) goto 8
257  GO TO 6
258  7 p=q-1
259  GO TO 13
260 !
261 ! RIGHT
262 !
263  8 q=q-1
264  IF (q.LE.p) GO TO 9
265  z=ord(q)
266 ! IF (A(Z).LE.A(ZZ)) GO TO 10
267  if( compf(z, zz) .le. 0) goto 10
268  GO TO 8
269  9 q=p
270  p=p-1
271  z=x
272  x=ord(p)
273 !
274 ! DIST
275 !
276 ! 10 IF (A(X).LE.A(Z)) GO TO 11
277  10 IF ( compf(x, z) .le. 0) goto 11
278  y=x
279  x=z
280  z=y
281  ip=ord(p)
282  ord(p)=ord(q)
283  ord(q)=ip
284 ! 11 IF (A(X).LE.A(XX)) GO TO 12
285  11 IF ( compf( x, xx) .le. 0) goto 12
286  xx=x
287  ix=p
288 ! 12 IF (A(Z).GE.A(ZZ)) GO TO 6
289  12 IF ( compf(z, zz) .ge. 0) goto 6
290  zz=z
291  iz=q
292  GO TO 6
293 !
294 ! OUT
295 !
296  13 CONTINUE
297 ! IF (.NOT.(P.NE.IX.AND.A(X).NE.A(XX))) GO TO 14
298  IF (.NOT.(p.NE.ix.AND. compf(x, xx) .ne. 0) ) goto 14
299  ip=ord(p)
300  ord(p)=ord(ix)
301  ord(ix)=ip
302  14 CONTINUE
303 ! IF (.NOT.(Q.NE.IZ.AND.A(Z).NE.A(ZZ))) GO TO 15
304  IF (.NOT.(q.NE.iz.AND. compf(z, zz) .ne. 0) ) goto 15
305  iq=ord(q)
306  ord(q)=ord(iz)
307  ord(iz)=iq
308  15 CONTINUE
309  IF (u-q.LE.p-l) GO TO 16
310  l1=l
311  u1=p-1
312  l=q+1
313  GO TO 17
314  16 u1=u
315  l1=q+1
316  u=p-1
317  17 CONTINUE
318  IF (u1.LE.l1) GO TO 18
319 !
320 ! START RECURSIVE CALL
321 !
322  ndeep=ndeep+1
323  poplst(1,ndeep)=u
324  poplst(2,ndeep)=l
325  GO TO 3
326  18 IF (u.GT.l) GO TO 4
327 !
328 ! POP BACK UP IN THE RECURSION LIST
329 !
330  IF (ndeep.EQ.0) GO TO 2
331  u=poplst(1,ndeep)
332  l=poplst(2,ndeep)
333  ndeep=ndeep-1
334  GO TO 18
335 !
336 ! END SORT
337 ! END QSORT
338 !
339  END
nodes z
nodes i
subroutine kqsort(compf, ORD, N)
Definition: kqsort.f:198
********************block data cblkHeavy ********************integer j data *HeavyG2symbol p
Definition: cblkHeavy.h:7
latitude latitude this system is used *****************************************************************! type coord sequence union map real y
Definition: Zcoord.h:25
integer n
Definition: Zcinippxc.h:1
! structure defining a particle at production ! Basic idea of what is to be contained in ! the particle structue is that dynamical ones should be included those derivable from the particle code ! is not included ******************************************************type fmom momentum sequence union map real e endmap map real * x
Definition: Zptcl.h:21