• Post Reply Bookmark Topic Watch Topic
  • New Topic

stack overflow error  RSS feed

 
Jeff Yan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, im getting a stack overflow error when i run my code! im doing a quicksort algorithm! any ideas??!! here is my code below:
>
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is wrong:

int x=data[(data.length - 1)];

x is always equal to the last element of the table,
so when the function is called for the first time (j=data.lenght-1), j
is never decremented in the 'while (data[j]>x)' loop,
and then quicksort is recursively called with boundaries: left, j =data.lenght-1 boundaries.
You must take 'x' from somwhere between 'left' and 'right' boundaries.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!