From RAD Studio API Documentation
Jump to: navigation, search


function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;


bool __fastcall BinarySearch(const T Item, /* out */ int &Index)/* overload */;
bool __fastcall BinarySearch(const T Item, /* out */ int &Index, const System::DelphiInterface<System::Generics::Defaults::IComparer__1<T> > AComparer)/* overload */;


Type Visibility Source Unit Parent
function public
System.Generics.Collections TList


Search a sorted list for an element using binary search.

The overloaded method BinarySearch searches for the list element Item using a binary search. The method returns True if it finds the element and False otherwise. If found, Index contains the zero-based index of Item. If not found, Index contains the index of the first entry larger than Item.

Note: BinarySearch requires that the list be sorted. The IndexOf method does not require a sorted list, but is usually slower than BinarySearch.

If there is more than one element in the list equal to Item, the index of the first match is returned in Index. This is the index of any of the matching items, not necessarily the first.

A comparison function AComparer may be provided to compare elements.

If Item is out of the range of the list, an EArgumentOutOfRangeException exception is raised.

This is an O(log n) operation for a list with n entries.

See Also

Code Examples