Add Elixir example for binary search
This commit is contained in:
committed by
Aditya Bhargava
parent
bb4a8638f1
commit
8ed03e73aa
29
01_introduction_to_algorithms/elixir/01_binary_search.exs
Normal file
29
01_introduction_to_algorithms/elixir/01_binary_search.exs
Normal file
@@ -0,0 +1,29 @@
|
||||
defmodule BinarySearch do
|
||||
def search(list, item) do
|
||||
low = 0
|
||||
high = length(list) - 1
|
||||
|
||||
do_search(list, item, low, high)
|
||||
end
|
||||
|
||||
defp do_search(_, _, low, high) when high < low, do: nil
|
||||
|
||||
defp do_search(list, item, low, high) do
|
||||
mid = div(low + high, 2)
|
||||
guess = Enum.at(list, mid)
|
||||
|
||||
cond do
|
||||
guess == item -> mid
|
||||
guess > item -> do_search(list, item, low, mid - 1)
|
||||
true -> do_search(list, item, mid + 1, high)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
my_list = [1, 3, 5, 7, 9]
|
||||
|
||||
IO.puts(BinarySearch.search(my_list, 3))
|
||||
# => 1
|
||||
|
||||
IO.inspect(BinarySearch.search(my_list, -1))
|
||||
# => nil
|
||||
Reference in New Issue
Block a user