943,815 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 970
  • Java RSS
Dec 3rd, 2008
0

Recursion help

Expand Post »
I'm working on a quick sort algorithm using recursion but it's throwing a stackOverflowError. Here is the code:
Java Syntax (Toggle Plain Text)
  1. public static void quicksort(int [] A){
  2. quickSortRec(A, 0, A.length-1);
  3. }
  4.  
  5.  
  6. public static void quickSortRec(int [] A, int p, int r){
  7.  
  8. if (p < r){
  9. int q = Partition(A, p, r);
  10. quickSortRec(A, p, q-1);
  11. quickSortRec(A, q+1, r);
  12. }
  13. }
  14.  
  15. public static int Partition(int[] A, int p, int r){
  16.  
  17. int x = A[r];
  18. int i = p-1;
  19. int j = p;
  20.  
  21. while (j < r){
  22. if (A[j] <= x){
  23. i++;
  24. int tmp = A[i];
  25. A[i] = A[j];
  26. A[j] = tmp;
  27. }
  28. j++;
  29. }
  30. int tmp = A[i+1];
  31. A[i+1] = A[r];
  32. A[r] = tmp;
  33.  
  34. return i+1;
  35. }

The thing that's puzzling me is there's no problems when I call for it from this piece of code:

Java Syntax (Toggle Plain Text)
  1. public class A5Q5 {
  2.  
  3. public static void main(String[] args){
  4.  
  5. //Length of array is given by command argument
  6. int length = Integer.parseInt(args[0]);
  7.  
  8. //100 arrays of variable lengths of random elements are created and then sorted
  9. for (int i=0; i < 100; i++){
  10. int[] arr = new int[length];
  11.  
  12. for (int j = 0; j < length; j++){
  13.  
  14. Random rand = new Random();
  15. int n = rand.nextInt(65578);
  16. arr[j] = n;
  17. }
  18. try{
  19. Sorting.heapSort(arr);}
  20. catch (EmptyHeapException ex){
  21. System.out.println("Heap is empty, unable to sort array.");
  22. }
  23. catch (OutOfRangeException ex){
  24. System.out.println("Error: Out of Range");
  25. }
  26. Sorting.insertionSort(arr);
  27. Sorting.quicksort(arr);
  28. Sorting.quicksortImproved(arr);
  29. Arrays.sort(arr);
  30. }
  31. }
  32. }

but a stackOverflowError is thrown when I try to call for it from this one:

Java Syntax (Toggle Plain Text)
  1. public class A5Q6 {
  2.  
  3. public static void main(String[] args){
  4.  
  5. //Length of array is given by command argument
  6. int length = Integer.parseInt(args[0]);
  7.  
  8. //100 arrays of variable lengths of elements of ascending values are created and then sorted
  9. for (int i=0; i < 100; i++){
  10. int[] arr = new int[length];
  11.  
  12. int num = 0;
  13. for (int j = 0; j < length; j++){
  14.  
  15. Random rand = new Random();
  16. int n = rand.nextInt(10);
  17.  
  18. num += n;
  19. arr[j] = num;
  20. }
  21.  
  22. try{
  23. Sorting.heapSort(arr);}
  24. catch (EmptyHeapException ex){
  25. System.out.println("Heap is empty, unable to sort array.");
  26. }
  27. catch (OutOfRangeException ex){
  28. System.out.println("Error: Index is out of range.");
  29. }
  30. Sorting.insertionSort(arr);
  31. Sorting.quicksort(arr);
  32. Sorting.quicksortImproved(arr);
  33. Arrays.sort(arr);
  34. }
  35. }
  36. }

Any help will be greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocku is offline Offline
22 posts
since Nov 2007
Dec 3rd, 2008
0

Re: Recursion help

I didn't realize that I was using different command line arguments for A5Q5 and A5Q6 until now. So it seems that my code works fine for arrays of smaller lengths (such as 128) but not for arrays of larger sizes, say 16328.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocku is offline Offline
22 posts
since Nov 2007
Dec 4th, 2008
0

Re: Recursion help

make stack profile
Last edited by quuba; Dec 4th, 2008 at 8:41 pm. Reason: stupid first reply
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 5th, 2008
0

Re: Recursion help

In quickSortRec(int [] A, int p, int r) You are using array as parameter, which makes Your code most not effectively. Every call cause store whole array on the stack.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Dec 13th, 2008
0

Re: Recursion help

Alright, thanks for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pocku is offline Offline
22 posts
since Nov 2007
Dec 14th, 2008
0

Re: Recursion help

> Every call cause store whole array on the stack.

No it doesn't. Arrays in Java are objects and when invoking methods with object references, copy of the reference is created and copied to the stack frame which is then pushed onto the call stack [the stack which maintains the state (stack frame) of the currently executing methods]. The actual array still lives in the heap.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 15th, 2008
0

Re: Recursion help

On average, you really wouldn't expect to perform so many recursions as to get a stack overflow error: even with your list size of 16,000, on average, you'd expect to go to around 14 or so levels of recursion (2^14 = 16384).

But in the WORST case, quicksort will require 'n' levels of recursion, and I think you're hitting this worst case. It looks like you're sorting the data and then always using the rightmost element as the pivot value. If you do that, then if you think about it, on each recursion you're picking the highest value in the sublist as the pivot value, so that instead of splitting the list roughly into two equal halves (on average what you'd expect if you chose a random pivot position), you're splitting it into "halves" that are (n-1) and 1 in length.

To get round this you need to either:
(1) guarantee that you won't pass already-sorted data to your method, or
(2) have a better pivot selection method (common strategies are to just pick a random index each time, or perform "quick heuristic" to choose a pivot value -- e.g. take the average of the numbers at x random indices).
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Arraylist Problem Help Please
Next Thread in Java Forum Timeline: Clock.java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC