Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions solutions/elixir/binary-search/2/lib/binary_search.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule BinarySearch do
@doc """
Searches for a key in the tuple using the binary search algorithm.
It returns :not_found if the key is not in the tuple.
Otherwise returns {:ok, index}.

## Examples

iex> BinarySearch.search({}, 2)
:not_found

iex> BinarySearch.search({1, 3, 5}, 2)
:not_found

iex> BinarySearch.search({1, 3, 5}, 5)
{:ok, 2}

"""

@spec search(tuple, integer) :: {:ok, integer} | :not_found
def search({}, _), do: :not_found
def search(numbers, key), do: get_index(numbers, key, tuple_size(numbers) - 1)

defp get_index(numbers, key, min \\ 0, max) do
case min + div(max - min, 2) do
index when elem(numbers, index) == key -> {:ok, index}
index when elem(numbers, index) > key and index < max -> get_index(numbers, key, min, index)
index when elem(numbers, index) < key and index > min -> get_index(numbers, key, index, max)
_ when elem(numbers, max) == key -> {:ok, max}
_ -> :not_found
end
end
end