stella v0.6.0 BinarySearch

Binary search is a search algorithm that finds the position of a target value within a sorted array.

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)

Specs

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

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