CorArray: BinarySearchEx

BinarySearchEx

Searches a range of elements in a one-dimensional sorted Arr for a value, using an optionally specified IComparer interface.



 Public Function BinarySearchEx(
	  ByRef Arr As Variant,
	  ByVal Index As Long,
	  ByVal Length As Long,
	  ByRef Value As Variant,
	  Optional ByVal Comparer As IComparer ) As Long

Parameters

Arr
[ByRef] Variant. The sorted one-dimensional array to search.
Index
[ByVal] Long. The starting index in the range to search.
Length
[ByVal] Long. The length of the range to search.
Value
[ByRef] Variant. The value to search for in the array.
Comparer
[ByVal] Optional. IComparer. The IComparer implementation to use when comparing elements.
-or-
Nothing to use the default Comparer implementation.

Return Values

Long -  The index of the specified Value in the specified array, if Value is found. If Value is not found and Value is less than one or more elements in Arr, a negative number which is the bitwise complement of the index of the first element that is larger than Value. If Value is not found and Value is greater than any of the elements in Arr, a negative number which is the bitwise complement of (the index of the last element plus 1).

Remarks

If the returned value is less than the lower bound of the array, then

If the returned value is less than the lower bound of the array, the value is a bitwise Not indication of where the value would have been found in the array. The following shows how to convert the result for a value that is not found.
Lower Bound of ArrConversion Method
ZeroIndex = Not Result
Non-zeroIndex = (Not (Result - LBound(Arr))) + LBound(Arr)

Exceptions

ExceptionCondition
ArgumentNullException Arr is uninitialized
RankException Arr is multidimensional.
ArgumentOutOfRangeException Index is less than the lower-bound of Arr.
-or-
Length is less than zero.
ArgumentException Value is of a type that is not compatible with the elements in Arr.
InvalidOperationException Value is not comparable to the elements in Arr.

See Also

Project CorLib Overview

Class CorArray Overview

BinarySearch