LoginSignup
2
2

More than 5 years have passed since last update.

Rubyで2分探索

Posted at
contains.rb
def contains(v, vs)
    if vs.length == 0
        return false;
    end
    left = 0; right = vs.length
    while left + 1 < right
        mid = left + (right - left) / 2;
        if v < vs[mid]
            right = mid
        else
            left = mid
        end
    end
    return v == vs[left];
end

vs = [*1..10000000]
p contains(100, vs)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2