System.Generics.Collections.TList.BinarySearch
Delphi
function BinarySearch(const Item: T; out FoundIndex: Integer): Boolean; overload;
function BinarySearch(const Item: T; out FoundIndex: Integer; const AComparer: IComparer<T>): Boolean; overload;
function BinarySearch(const Item: T; out FoundIndex: Integer; const AComparer: IComparer<T>; Index, Count: Integer): Boolean; overload;
C++
bool __fastcall BinarySearch(const T Item, /* out */ int &FoundIndex)/* overload */;
bool __fastcall BinarySearch(const T Item, /* out */ int &FoundIndex, const System::DelphiInterface<System::Generics::Defaults::IComparer__1<T> > AComparer)/* overload */;
bool __fastcall BinarySearch(const T Item, /* out */ int &FoundIndex, const System::DelphiInterface<System::Generics::Defaults::IComparer__1<T> > AComparer, int Index, int Count)/* overload */;
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
function | public | System.Generics.Collections.pas System.Generics.Collections.hpp |
System.Generics.Collections | TList |
Description
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