Created
October 16, 2018 11:13
-
-
Save thiagoa/41cf9452c80ac1e25a52078ed33109b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule BinarySearch do | |
def find(item, _left, [i | _right]) when i == item, do: true | |
def find(item, _left, [i | right]) when i < item, do: find(item, right) | |
def find(item, left, [i | _right]) when i > item, do: find(item, left) | |
def find(_item, _left, []), do: false | |
def find(item, list) do | |
{left, right} = split(list) | |
find(item, left, right) | |
end | |
def split(list) do | |
len = round(length(list) / 2) | |
case Enum.split(list, len) do | |
{left, []} -> {[], left} | |
res -> res | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment