You are here: Symbol Reference > AbstractMtxVec Namespace > Classes > TMtxVec Class > public > BinarySearch Method > TMtxVec.BinarySearch Method (double, integer)
MtxVec VCL
ContentsIndex
PreviousUpNext
TMtxVec.BinarySearch Method (double, integer)

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

Pascal
function BinarySearch(const x: double; var XIndex: integer): boolean; overload;

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.

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);
Copyright (c) 1999-2024 by Dew Research. All rights reserved.
What do you think about this topic? Send feedback!