Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created March 7, 2016 20:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kenmazaika/e13ae817e133e15e87c7 to your computer and use it in GitHub Desktop.
Save kenmazaika/e13ae817e133e15e87c7 to your computer and use it in GitHub Desktop.
def binary_search(target, list)
position = (list.count / 2).floor
mid = list[position]
return mid if mid == target
if(mid < target)
return binary_search(target, list.slice(position + 1, list.count/2))
else
return binary_search(target, list.slice(0, list.count/2))
end
end
puts binary_search(9, [1,2,3,4,5,6,7,8,9,10])
@lightningwind
Copy link

Here is a recursive implementation of binary search which is based on divide and conquer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment