C+ C NAME: C nrHunt C PURPOSE: C Find the position of X in array XX, starting from an initial guess C CATEGORY: C Math: sorting C CALLING SEQUENCE: subroutine nrHunt(N,XX,X,JLO) C INPUTS: C N integer dimension of array XX C XX(N) real array. MUST be monotonic. C X real value for which position in XX is required C JLO integer initial guess for position of X C OUTPUTS: C JLO integer array index such that X lies between C XX(JLO) and XX(JLO+1) C RESTRICTIONS: C The array XX must be monotonic (increasing or decreasing). C NO CHECK IS MADE TO ENSURE THE ARRAY IS MONOTONIC. C Call SortR4 if you are not sure the array is sorted. C PROCEDURE: C See Numerical Recipes, par. 3.4, p. 91 C (see also subroutine nrLocate) C MODIFICATION HISTORY: C JUN-1993, Paul Hick (UCSD/CASS; pphick@ucsd.edu) C- integer N real XX(N) real X integer JLO logical ASCND logical GO ASCND = XX(N) .gt. XX(1) GO = .TRUE. if (JLO .le. 0 .or. JLO .gt. N) then ! Go straight to bisection JLO = 0 JHI = N+1 GO = .FALSE. end if if (GO) then INC = 1 if (X .ge. XX(JLO) .eqv. ASCND) then GO = .TRUE. JHI = JLO+INC do while (JHI .le. N .and. (X .ge. XX(JHI) .eqv. ASCND) ) JLO = JHI INC = INC+INC JHI = JLO+INC end do JHI = min(JHI,N+1) else JHI = JLO JLO = JHI-INC do while (JLO .ge. 1 .and. (X .lt. XX(JLO) .eqv. ASCND) ) JHI = JLO INC = INC+INC JLO = JHI-INC end do JLO = max(JLO,1) end if end if do while (JHI-JLO .ne. 1) JM = (JHI+JLO)/2 if (X .gt. XX(JM) .eqv. ASCND) then JLO = JM else JHI = JM end if end do return end