I am looking for a built-in Ruby method that has the same functionality as index
but uses a binary search algorithm, and thus requires a pre-sorted array.
I know I could write my own implementation, but according to "Ruby#index Method VS Binary Search", the built-in simple iterative search used by index is faster than a pure-Ruby version of binary search, since the built-in method is written in C.
Does Ruby provide any built-in methods that do binary search?
bsearch
gem, as Marc-André suggested. Then it's pretty much as simple asgem install bsearch
on the command line, andrequire 'bsearch'
in your Ruby. You might want to look at the documentation for usage. – Derk