Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Created October 16, 2018 11:13
Show Gist options
  • Save thiagoa/41cf9452c80ac1e25a52078ed33109b4 to your computer and use it in GitHub Desktop.
Save thiagoa/41cf9452c80ac1e25a52078ed33109b4 to your computer and use it in GitHub Desktop.
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