From: Ken Bloom Date: 2009-08-10T23:08:33+09:00 Subject: Re: what is "stack level too deep (SystemStackError)" error On Sat, 08 Aug 2009 19:39:35 +0900, Josh Cheek wrote: > On Fri, Aug 7, 2009 at 6: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 via http://www.ruby-forum.com/. >> >> > Hi, you have two issues with your algorithm, that I see. Here is what I > would do http://pastie.org/576485 it explains what the two issues are, > shows how I would change the code to fix them, as well as a few other > small changes to make the code more usable, then runs through a few > tests to make sure that it is doing what we think it is doing. > > In some browsers, pastie seems to word wrap, so if it does that for you, > you should probably c&p it into your text editor for reading. You can't write this all in your message so that it can be permenantly archived wherever the ruby-talk mailing list is mirrored? What happens if the pastebin shuts down in the future (e.g. some time next week)? Nobody will ever know the answer you gave. -- Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/