C+ C NAME: C IndexChar C PURPOSE: C Return index table for given one-dimensional array C CATEGORY: C Math: sorting C CALLING SEQUENCE: subroutine IndexChar(N1,N2,M1,M2,cArr,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 cArr(M1:M2)*(*) character 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 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 character cArr(M1:M2)*(*) integer INDX(M1:M2) character Q*500 lenQ = len(cArr(M1)) 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 = cArr(INDXT) else INDXT = INDX(IR) Q = cArr(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 (cArr(INDX(J)) .lt. cArr(INDX(J+1))) J = J+1 end if if (Q(:lenQ) .lt. cArr(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