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
49 changes: 49 additions & 0 deletions solutions/elixir/binary-search/1/lib/binary_search.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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(numbers, key) do
delta = numbers |> tuple_size() |> div(2)
get_index(numbers, key, delta, delta)
end

defp get_index(numbers, key, index, delta, checked \\ [])
defp get_index(numbers, key, index, _, _) when elem(numbers, index) == key, do: {:ok, index}
defp get_index(_, _, _, 0, _), do: :not_found

defp get_index(numbers, key, index, delta, checked) when elem(numbers, index) > key do
if index in checked do
:not_found
else
half = div(delta + 1, 2)
get_index(numbers, key, index - half, half, [index | checked])
end
end

defp get_index(numbers, key, index, delta, checked) when elem(numbers, index) < key do
if index in checked do
:not_found
else
half = div(delta + 1, 2)
get_index(numbers, key, index + half, half, [index | checked])
end
end

defp get_index(_, _, _, _, _), do: :not_found
end