Skip to content

Instantly share code, notes, and snippets.

@mario4000
Forked from kenmazaika/binary_search.rb
Created October 18, 2017 10:15
Show Gist options
  • Save mario4000/5121921e357d070b1c5517605c3ad152 to your computer and use it in GitHub Desktop.
Save mario4000/5121921e357d070b1c5517605c3ad152 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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment