ArrayBsearch
ArrayBsearch
Searches for a specified value in a multidimensional numeric array sorted in the ascending order. The search is performed in the first dimension taking into account the AS_SERIES flag.
For searching in an array of double type
int ArrayBsearch(
const double& array[], // array for search
double value, // what is searched for
int count=WHOLE_ARRAY, // count of elements to search for
int start=0, // starting position
int direction=MODE_ASCEND // search direction
);For searching in an array of float type
int ArrayBsearch(
const float& array[], // array for search
float value, // what is searched for
int count=WHOLE_ARRAY, // count of elements to search for
int start=0, // starting position
int direction=MODE_ASCEND // search direction
);For searching in an array of long type
int ArrayBsearch(
const long& array[], // array for search
long value, // what is searched for
int count=WHOLE_ARRAY, // count of elements to search for
int start=0, // starting position
int direction=MODE_ASCEND // search direction
);For searching in an array of int type
int ArrayBsearch(
const int& array[], // array for search
int value, // what is searched for
int count=WHOLE_ARRAY, // count of elements to search for
int start=0, // starting position
int direction=MODE_ASCEND // search direction
);For searching in an array of short type
int ArrayBsearch(
const short& array[], // array for search
short value, // what is searched for
int count=WHOLE_ARRAY, // count of elements to search for
int start=0, // starting position
int direction=MODE_ASCEND // search direction
);For searching in an array of char type
int ArrayBsearch(
const char& array[], // array for search
char value, // what is searched for
int count=WHOLE_ARRAY, // count of elements to search for
int start=0, // starting position
int direction=MODE_ASCEND // search direction
);Parameters
- array[]
[in] Numeric array for search.
- value
[in] Value for search.
- count=WHOLE_ARRAY
[in] Count of elements to search for. By default, it searches in the whole array.
- start=0
[in] Starting index to search for. By default, the search starts at the first element.
- direction=MODE_ASCEND
[in] Search direction. It can be any of the following values:
MODE_ASCEND searching in forward direction, MODE_DESCEND searching in backward direction.
Return Value
The function returns index of a found element. If the wanted value isn’t found, the function returns the index of an element nearest in value.
Note
Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function.
Example:
datetime daytimes[];
int shift=10,dayshift;
// All the Time[] series are sorted in descendant mode
ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
if(Time[shift]>=daytimes[0]) dayshift=0;
else
{
dayshift=ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
if(Period()<PERIOD_D1) dayshift++;
}
Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ",
TimeToStr(daytimes[dayshift]));