BinarySearch (stella v0.7.0)
Binary search is a search algorithm that finds the position of a target value within a sorted array.
annotations
Annotations
- n - number of elements in list
Link to this section Summary
Functions
Find the number in the list and return its index. Requires a sorted list as input
Link to this section Functions
Link to this function
run(list, searched_value)
@spec run(list(), number()) :: :not_found | non_neg_integer()
Find the number in the list and return its index. Requires a sorted list as input
- Time complexity: O(log2n)
- Memory complexity: O(1)
examples
Examples
iex> BinarySearch.run([-1, 2, 3.00000001, 4], 3.00000001)
2
iex> BinarySearch.run([0, 0, 0, 0, 1, 2, 3, 4], 0)
3
iex> BinarySearch.run([-5, 0, 0, 1, 1, 2, 3, 4], 5)
:not_found