C+ C NAME: C IndexR4 C PURPOSE: C Return index table for given one-dimensional array C CATEGORY: C Math: sorting C CALLING SEQUENCE: subroutine IndexR4(N1,N2,M1,M2,ARRIN,INDX) C INPUTS: C N1,N2 integer logical dimensions of arrays ARRIN and INDX C M1,M2 integer physical dimensions of arrays ARRIN and INDX C (only the logical part of the array is indexed). C ARRIN(M1:M2) real array to be indexed C OUTPUTS: C INDX(M1:M2) integer index table C SIDE EFFECTS: C The part of the index array INDX outside the logical range (i.e. C [M1:N1-1] and [N2+1,M2]) remains untouched C RESTRICTIONS: C The index range [N1,N2] must be a subset of [M1,M2] C SEE ALSO: C IndexI2, IndexI4, IndexR8, SortR4, Sort2R4, Rank C PROCEDURE: C Only the part ARRIN(N1:N2) is indexed. The array index values [N1,N2] C are arranged in INDX(N1:N2) in such a way that after indexing INDX(N1) C contains the array index of the smallest element of ARRIN(N1:N2) and C INDX(N2) the array index of the largest element of ARRIN(N1:N2). C Generalization of the heapsort indexing subroutine in Press et al., C "Numerical Recipes", Cambridge UP (1989), Chapter 8, p. 233. C MODIFICATION HISTORY: C 1990, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- integer N1 integer N2 integer M1 integer M2 real*4 ARRIN(M1:M2),Q integer INDX(M1:M2) do J=N1,N2 INDX(J) = J end do if (N2 .eq. N1) return ! Only one element to be indexed L = (N2-N1+1)/2+N1 IR = N2 10 if (L .gt. N1) then L = L-1 INDXT = INDX(L) Q = ARRIN(INDXT) else INDXT = INDX(IR) Q = ARRIN(INDXT) INDX(IR) = INDX(N1) IR = IR-1 if (IR .eq. N1) then INDX(N1) = INDXT return end if end if I = L J = L+L-N1+1 do while (J .le. IR) if (J .lt. IR) then if (ARRIN(INDX(J)) .lt. ARRIN(INDX(J+1))) J = J+1 end if if (Q .lt. ARRIN(INDX(J))) then INDX(I) = INDX(J) I = J J = J+J-N1+1 else J = IR+1 end if end do INDX(I) = INDXT go to 10 end