From: pharrington Date: 2009-08-08T09:25:04+09:00 Subject: Re: what is "stack level too deep (SystemStackError)" error On Aug 7, 7:52 pm, Mrmaster Mrmaster wrote: > 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) > -- > Posted viahttp://www.ruby-forum.com/. When coding recursively, you *always* need to test for the exit condition first. With searches, this means ensuring your result set isn't empty before continuing. If your code still loops endlessly after doing this, it helps to print the search set each step of the way to see exactly whats going wrong. (in this case your set is actually the bounds passed to the method, not the array itself ***)