Skip to content

Instantly share code, notes, and snippets.

@dalibor
Created April 23, 2010 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalibor/376285 to your computer and use it in GitHub Desktop.
Save dalibor/376285 to your computer and use it in GitHub Desktop.
IP array search
ip = 12
arr = [1, 3, 5, 7, 9, 10, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 34, 35, 36]
index = arr.length
min = 0; max = arr.length
while (min + 1 != max) do
index = (min + max) / 2
if ip < arr[index]
max = index
else
min = index
end
puts "Step: index = #{"%3d" % index}; value = #{"%3d" % arr[index]}; min = #{"%3d" % min}; max = #{"%3d" % max}"
end
puts "\nResult: index = #{"%3d" % min}; value range = #{arr[min]}..#{arr[max]};"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment