1
subroutine wsort(countr,counti,n,index,test)
4
c wsort sort double precision array according to rule specified by
6
c maintaining an index array
9
c subroutine wsort(countr,counti,n,index,test)
11
c double precision count(n)
15
c count(r,i) : array to be sorted
16
c n :size of count and index
17
c index : array containing on return index of sorted array
18
c test : external integer function which define formal order for
22
c r1,i1 are real and imag part of first complex number
23
c r2,i2 are real and imag part of second complex number
25
c 1 :if 1 is greater than 2
26
c -1 :if 1 is less than 2
27
c 0 :if 1 is equal to 2
30
c quick sort metjod is used
32
c n must be less than 2**(50/2) ! due to lengh of work space mark
35
dimension mark(50),index(n)
36
double precision countr(n),counti(n),avr,avi,xr,xi
40
c set index array to original order .
44
c check that a trivial case has not been entered .
48
c 'm' is the length of segment which is short enough to enter
49
c the final sorting routine. it may be easily changed.
51
c set up initial values.
56
c if segment is short enough sort with final sorting routine .
58
if((ifka+1).gt.m)goto 70
59
c********* final sorting ***
60
c ( a simple bubble sort )
64
40 it=test(countr(i-1),counti(i-1),countr(i),counti(i))
67
if(index(i-1).lt.index(i))goto 60
82
c ******* quicksort ********
83
c select the number in the central position in the segment as
84
c the test number.replace it with the number from the segment's
93
c the markers 'i' and 'ifk' are used for the beginning and end
94
c of the section not so far tested against the present value
98
c we alternate between the outer loop that increases i and the
99
c inner loop that reduces ifk, moving numbers and indices as
100
c necessary, until they meet .
102
it=test(xr,xi,countr(i),counti(i))
105
if(intest.gt.index(i))goto 110
106
80 if(i.ge.ifk)goto 120
107
countr(ifk)=countr(i)
108
counti(ifk)=counti(i)
113
it=test(countr(ifk),counti(ifk),xr,xi)
116
if(intest.le.index(ifk))goto 100
117
90 if(i.ge.ifk)goto 130
118
countr(i)=countr(ifk)
119
counti(i)=counti(ifk)
125
c return the test number to the position marked by the marker
126
c which did not move last. it divides the initial segment into
127
c 2 parts. any element in the first part is less than or equal
128
c to any element in the second part, and they may now be sorted
139
c store the longer subdivision in workspace.
140
140 if((ip-is).gt.(if-ip))goto 150
148
c find the length of the shorter subdivision.
150
if(lngth.le.0)goto 180
151
c if it contains more than one element supply it with workspace .
154
170 if(la.le.0)goto 200
155
c obtain the address of the shortest segment awaiting quicksort
161
integer function rptest(r1,i1,r2,i2)
162
double precision r1,i1,r2,i2
165
elseif(r1.lt.r2) then
171
integer function modtest(r1,i1,r2,i2)
172
double precision r1,i1,r2,i2,a1,a2
177
elseif(a1.lt.a2) then