Support

Equilla Formula Language

BinarySearchArray

Returns the position of the first element with a specific value in an array using a binary search, or -1 if the element could not be found. The vector must be sorted in ascending order for this search to work correctly. This search is significantly faster for large arrays than the normal SearchArray function, the limitation is the vector must be sorted. If the array is not sorted or the wrong sort order is given, the function may not find the value even if it is in the array.

Inputs

Array
Array: The array to search.
ValueToFind
Any: The value to look for in the array.
Order
Boolean: (Optional) True if the elements are sorted in ascending order. The default value for this is True.

Examples

Find the position of the first element having the number 5
Variable: result; Array: myDynamicArray[]; FillArray(myDynamicArray, 5, 6, 2, 5, 1, 2); SortArray(myDynamicArray); print(myDynamicArray); // prints 1 2 2 5 5 6 result = BinarySearchArray(myDynamicArray, 5); print(result); // prints 4
Behaviour if array is not sorted
Variable: result; Array: myDynamicArray[]; FillArray(myDynamicArray, 5, 6, 2, 5, 1, 2); print(myDynamicArray); // prints 5 6 2 5 1 2 result = BinarySearchArray(myDynamicArray, 5); print(result); // prints -1