Skip to content

Instantly share code, notes, and snippets.

@Chris927
Created July 8, 2011 09:42
Show Gist options
  • Select an option

  • Save Chris927/1071464 to your computer and use it in GitHub Desktop.

Select an option

Save Chris927/1071464 to your computer and use it in GitHub Desktop.
def chop(v, values)
ix = values.length / 2
m = values[ ix ]
return ix if v == m
return nil if values.length < 2
if v > m
ix + 1 + chop(v, values[ix+1..-1]) rescue nil
else
chop(v, values[0..ix-1]) rescue nil
end
end
a = [ 1, 4, 7, 13, 54, 88, 102 ]
puts chop(0, a)
puts chop(1, a)
puts chop(4, a)
puts chop(7, a)
puts chop(14, a)
puts chop(88, a)
puts chop(102, a)
puts chop(103, a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment