Matrix.BinarySearch Method

Overload List

#SignatureDescription
1function BinarySearch(const X: Double): Integer;Finds a match for X in sorted object values using binary search.
2function BinarySearch(const X: Double; var XIndex: Integer): Boolean;Finds exact or closest Index match for X in sorted object values using binary search.

Overload 1: function BinarySearch(const X: Double): Integer;

Finds a match for X in sorted object values using binary search.

#NameTypeDescription
1XDouble

Returns: Int32 - the Index of last matched element. If no matching elements are found, the result is -1.

Remarks:

The data in the vector must be sorted in ascending order for this function to work correctly.

Overload 2: function BinarySearch(const X: Double; var XIndex: Integer): Boolean;

Finds exact or closest Index match for X in sorted object values using binary search.

#NameTypeDescription
1XDouble
2XIndexIntegerX start index

Returns: Boolean - True and exact Index in XIndex, if found and False and the Index of the next bigger or smaller value in XIndex, if not found.

Remarks:

The data in the vector must be sorted in ascending order for this function to work correctly. The closest match is the Index of first bigger or smaller value in the array.

To ensure bigger value write:

Data := [0,2,3];
Data.BinarySearch(Value, XIndex);
if Data[XIndex] > Value then Dec(XIndex);

To ensure smaller value write:

Data := [0,2,3];
Data.BinarySearch(1, XIndex);
if Data[XIndex] < Value then Inc(XIndex);