M
Mrmaster Mrmaster
Hi,
I've created a binary search using code from wiki and when i run it i
get there error stated below. I'm only using 10 entries so i'm not sure
why it would give me that error. How can I modify my code to handle 100k
entries?
test.rb:14:in `binSearch': stack level too deep (SystemStackError)
from test.rb:14:in `binSearch'
from test.rb:20
Here is my code:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 , 11, 12, 13, 14, 15]
target = 6
left = 0
right = (array.length)
def binSearch(left, right, target, array)
middle = left + ((right - left) / 2)
if array[middle] == target then
puts target
elsif array[middle] > target then
return binSearch(left, middle, target, array)
elsif array[middle] < target then
return binSearch(middle, right, target, array)
else
puts "Target does not exist"
end
end
binSearch(left, right, target, array)
I've created a binary search using code from wiki and when i run it i
get there error stated below. I'm only using 10 entries so i'm not sure
why it would give me that error. How can I modify my code to handle 100k
entries?
test.rb:14:in `binSearch': stack level too deep (SystemStackError)
from test.rb:14:in `binSearch'
from test.rb:20
Here is my code:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10 , 11, 12, 13, 14, 15]
target = 6
left = 0
right = (array.length)
def binSearch(left, right, target, array)
middle = left + ((right - left) / 2)
if array[middle] == target then
puts target
elsif array[middle] > target then
return binSearch(left, middle, target, array)
elsif array[middle] < target then
return binSearch(middle, right, target, array)
else
puts "Target does not exist"
end
end
binSearch(left, right, target, array)