Module: Ruborithms::Algorithms::BinarySearch::ClassMethods

Defined in:
lib/ruborithms/algorithms/binary_search.rb

Instance Method Summary collapse

Instance Method Details

#binary_search(object, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruborithms/algorithms/binary_search.rb', line 11

def binary_search(object, value)
  min = 0
  max = object.count - 1
  while max >= min
    avg = ((max + min) / 2).floor
    return avg if object[avg] == value
    if object[avg] > value
      max = avg - 1
    else
      min = avg + 1
    end
  end
end