• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

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: 40678
827
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: 22262
119
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: 12992
66
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: 40678
827
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: 40678
827
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic