BinarySearchRange(
Value: Variant;
Comparer: IComparer;
Index: Integer;
Count: Integer): Integer;
Value. Value that should be found.
Comparer. An object that compares array elements.
Index. Initial index of the range, where the search is executed.
Count. Number of the elements that are used in search.
The BinarySearchRange method executes binary search of the element in array range.
Search is executed in the sorted array range. In case of successful search it returns the number of the first found element, otherwise it returns -1.
Sub UserProc;
Var
ArrayL: IArrayList;
i: Integer;
Begin
ArrayL := New ArrayList.Create;
For i := 0 To 100 Do
ArrayL.Add(Math.RandBetweenI(0, 50));
End For;
ArrayL.Sort(Comparer.IntegerComparer);
i := ArrayL.BinarySearchRange(5, Comparer.IntegerComparer, 10, ArrayL.Count - 20);
End Sub UserProc;
After executing the example a dynamic array of random numbers is generated. In the midsection of the array, except for first and last 10 elements, the search of the 5 value is executed. In case of successful search, the "i" variable contains the number of the first found element.
See also: