C+ C NAME: C Sort2R4 C PURPOSE: C Rearrange two array so that the first one is put into ascending C numerical order C CATEGORY: C Math: sorting C CALLING SEQUENCE: subroutine Sort2R4(N1,N2,M1,M2,RA,RB) C INPUTS: C N1,N2 integer logical dimensions of array RA C M1,M2 integer physical dimensions of array RA C (only the logical part of the array is sorted C RA(M1:M2) real array to be sorted C RB(M1:M2) real array to be sorted C OUTPUTS: C RA,RB sorted array C SIDE EFFECTS: C The part of the arrays RA,RB outside the logical range (i.e. indices C [M1,N1-1] and [N2+1,M2] remains untouched C RESTRICTIONS: C The logical range [N1,N2] must be a subset of physical range [M1,M2] C SEE ALSO: C Sort2I2, Sort2I4, Sort2R8, SortR4, Rank, IndexR4 C PROCEDURE: C Modification of the heapsort subroutine in Press et al., "Numerical C Recipes", Cambridge UP (1989), par. 8.2, p. 231 C MODIFICATION HISTORY: C 1990, Paul Hick (UCSD) C- integer N1 integer N2 integer M1 integer M2 real*4 RA(M1:M2) real*4 RB(M1:M2) real*4 RRA real*4 RRB if (N1 .lt. M1 .or. N2 .gt. M2) stop 'Sort2R4: error in array dimensions' L = (N2-N1+1)/2+N1 IR = N2 10 if (L .gt. N1) then L = L-1 RRA = RA(L) RRB = RB(L) else RRA = RA(IR) RRB = RB(IR) RA(IR) = RA(N1) RB(IR) = RB(N1) IR = max(N1,IR-1) ! 'max' needed to sort one element (N1=N2) if (IR .eq. N1) then RA(N1) = RRA RB(N1) = RRB return end if end if I = L J = L+L-N1+1 do while (J .le. IR) if (J .lt. IR) then if (RA(J) .lt. RA(J+1)) J=J+1 end if if (RRA .lt. RA(J)) then RA(I) = RA(J) RB(I) = RB(J) I = J J = J+J-N1+1 else J = IR+1 end if end do RA(I) = RRA RB(I) = RRB go to 10 end