E106: Binary Search for Element in Ordered Array

Author(s): F. James, K.S. Kölbig Library: KERNLIB
Submitter: Submitted: 18.10.1974
Language: Fortran Revised: 15.11.1995

Integer function subprograms LOCATI and LOCATR perform a binary search in an array of non-decreasing integer or real numbers tex2html_wrap_inline96 to locate a specified value t.

Structure:

FUNCTION subprograms
User Entry Names: LOCATI, LOCATR
Obsolete User Entry Names: LOCATF tex2html_wrap_inline100 LOCATR

On CDC or Cray computers, the double-precision version LOCATD is not available.

Usage:

In any arithmetic expression, for tex2html_wrap_inline102 (type INTEGER), tex2html_wrap_inline104 (type REAL), tex2html_wrap_inline106 (type DOUBLE PRECISION),

LOCATt(tA,N,tT)

has the INTEGER value according to the description below.
tA
(type according to t) One-dimensional array. The numbers tA(j) must form a non-decreasing sequence for tex2html_wrap_inline108 .
N
(INTEGER) Number n of elements in array tA.
tT
(type according to t) Search value t.
Depending on four possible outcomes of the search, each subprogram returns the following value L tex2html_wrap_inline114 , tex2html_wrap_inline116 ):

tex2html_wrap_inline118 for some j with tex2html_wrap_inline122 tex2html_wrap_inline124
tex2html_wrap_inline126 tex2html_wrap_inline128
tex2html_wrap_inline130 for some k with tex2html_wrap_inline134 tex2html_wrap_inline136
tex2html_wrap_inline138 tex2html_wrap_inline140

If the value t occurs more than once in the array a, the result L may correspond to any of the occurrences.

Method:

Repeated bisection of the subscript range.

Notes:

  1. The number of comparisons performed is approximately proportional to tex2html_wrap_inline146 . Therefore, for large N the binary search is considerably faster than a sequential search using a DO loop. However, for N less than about 40 a DO loop is faster.
  2. The obsolete older entry LOCATF is kept for a transitional period. It will eventually disappear.
tex2html_wrap_inline148

Michel Goossens Wed Jun 5 02:01:19 METDST 1996