• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

quick sort- stack overflow error?

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13073
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic