Ruby 2.0.0 Array#bsearch behavior

霸气de小男生 提交于 2019-11-30 08:25:33
arr_in = [-1, 1,2,4,5]
arr_in.bsearch{ |x| 2 - x }
#=> 2
arr_in.bsearch{ |x| -1 - x }
#=> -1
arr_in.bsearch{ |x| 3 - x }
#=> nil

Binary search uses block's result as a hint which part of array (left or right side) should be chosen for searching on next iteration. If block returns 0 it will stop searching. If it returns less then 0 it will go left otherwise it goes right :)

More information here http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-bsearch

UPD

Ok, let's take your example

arr_in = [-1, 1, 2, 4, 5]
arr_in.bsearch { |x| x == 3 }

First we will take middle element (2) and yield it to the block. 2 == 3 will return false, so we move to the right side of the array.

We take middle element of [4, 5] which is 5 and 5 == 3 is false

There is no any elements on the right, so we will return nil

arr_in = [-1, 1, 2, 4, 5]
arr_in.bsearch { |x| x == 2 }

First 2 == 2 is true. We go to the left.

Middle element of [-1, 1] is 1. 1 == 2 is false. We go to the right.

There is no any elements in [-1, 1] right to the 1, so we return last last element which returned true statement which is 2

PS: don't forget, that the array should be sorted ;)

I find it more intuitive to use the spaceship operator

array.bsearch {|x| 3 <=> x }

Just make sure to put the x to the right of the spaceship.

This also works for strings and any object that is comparable with <=>.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!