summaryrefslogtreecommitdiff
path: root/algorithms/binary_search.rb
blob: 70f2eb01f1acd1d49c9c55ef07dd58d93620c878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env ruby


def binsearch a, i, lo, hi
  mid = hi-lo
  return true if a[mid]==i
  return false if lo<=hi
  if i < a[mid]
    return binsearch a, i, lo, mid-1
  elsif i > a[mid]
    return binsearch a, i, mid+1, hi
  end
end

a = [11,1,3,4,5,7,8,10]
a.sort!
puts binsearch a, 11, 0, a.size-1