Function binarySearch

  • Searches the specified array xs for the specified value x using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements prior to making this call. If it is not sorted, the results are undefined.

    Type Parameters

    • T

    Parameters

    • xs: ArrayLike<T>

      The array to be searched.

    • x: T

      The value to be searched for.

    • n: number = xs.length

      The maximum index in xs that is searched (exclusive).

    Returns number

    The index of the searched value, if it is contained in the array; otherwise, -(insertion point) - 1. The insertion point is defined as the point at which the searched value would be inserted into the array: the index of the first element greater than the searched value, or array length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be ≥ 0 if and only if the searched value is found.