Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

quick sort- stack overflow error?

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have been working on writing a java quicksort using arrays, but ran into a stack overflow error. This is one of my first projects in recursion, and I'm not sure what I'm doing wrong. Would anyone mind checking over my code? Thanks!




Thank you very much for any help you can give!!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34375
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theresa,
First a tip for debugging - print out the values of j and k before making the recursive call to qsort. This will show you what number it is using when it gets into the infinite sequence.

Without running it, my guess is zero. I don't see an if statement around the recursive calls. This means the code doesn't know what the base case and therefore when it should stop making recursive calls.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Without running it, my guess is zero. I don't see an if statement around the recursive calls. This means the code doesn't know what the base case and therefore when it should stop making recursive calls.

Wouldn't that be the part?

Still, you're right in j and k never changing. I've tested it with a random array and j and k never change once j hits 0.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theresa,

I edited your post to break your comment into two lines. Our site renders a one-line comment on one line, so super-long ones tend to make it format strangely. It's usually easier to read this way, rather than having to scroll horizontally.

Just a tip for next time.

Thanks!!!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34375
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Jeanne Boyarsky wrote:Without running it, my guess is zero. I don't see an if statement around the recursive calls. This means the code doesn't know what the base case and therefore when it should stop making recursive calls.

Wouldn't that be the part?

Still, you're right in j and k never changing. I've tested it with a random array and j and k never change once j hits 0.

Rob,
I actually missed the if statement. I tried with the array "1,2,3" and it infinitely goes on j = 0, k = 2. The interesting part is that small is length 2 and large is length 0. Small never gets smaller which means the array is never <= 1.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34375
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two other notes - not about your problem, but about coding in general.

1) It is traditional for classnames to use camel case and begin with a capital letter. So it would be QuickSort or at least Quicksort.
2) Your constructor ignores the array it is passed. You don't actually need to write a constructor to use instance methods. Java provides you one by default that looks like this. Or of course you could write a zero argument constructor yourself.

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, everyone, for your suggestions/ help- I will be sure to try them all out- now I at least have a general idea of what needs to be fixed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic