=begin
It would be quite handy to have a method for searching through an Array, when sorted, using binary search.
Apparently doing this in C would be ideal [1].
So this is a feature request for the same
Array#binary_index
Array#binary_find (or binary_search, whichever the commiter prefers).
=begin
The point is, Ruby's arrays are not always sorted. You can't easily say if an array is sorted or not unless you actually scan the whole array, which is O(n) -- slower than binary search. And if you apply bsearch to non-sorted arrays it should of course fail.
As a standard API's behaviour "input is always sorted" kind of assumption is hard to accept I think.
=end
Thank you for reminding this.
I think this is a duplicate (or a subset) of #4766 which is accepted by matz.
So I'm going to commit my patch within a reasonable period of time :-)
This issue was solved with changeset r37655.
Yusuke, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
array.c (rb_ary_bsearch): add Array#bsearch for binary search. [ruby-core:36390] [Feature #4766]
test/ruby/test_array.rb: add a test for above.
range.c (range_bsearch): add Range#bsearch for binary search. [ruby-core:36390] [Feature #4766]