IArrayList.BinarySearch

Syntax

BinarySearch(Value: Variant; Comparer: IComparer): Integer;

Parameters

Value. Value that should be found.

Comparer. An object that compares array elements.

Description

The BinarySearch method executes binary search of elements in array.

Comments

Search is executed in the sorted array. In case of successful search it returns the number of the first found element, otherwise it returns -1.

Example

Sub Main;

Var

ArrayL: IArrayList;

i: Integer;

Begin

ArrayL:=New ArrayList.Create;

For i:=0 To 100 Do

ArrayL.Add(Math.RandBetweenI(0,100));

End For;

ArrayL.Sort(Comparer.IntegerComparer);

i:=ArrayL.BinarySearch(34,Comparer.IntegerComparer);

End Sub Main;

After executing the example a dynamic array of random numbers is generated, and the 34 value is searched in it. In case of successful search, the "i" variable contains the number of the first found element.

See also:

IArrayList