Recursion help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 22
Reputation: pocku is an unknown quantity at this point 
Solved Threads: 0
pocku pocku is offline Offline
Newbie Poster

Recursion help

 
0
  #1
Dec 3rd, 2008
I'm working on a quick sort algorithm using recursion but it's throwing a stackOverflowError. Here is the code:
  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:

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 22
Reputation: pocku is an unknown quantity at this point 
Solved Threads: 0
pocku pocku is offline Offline
Newbie Poster

Re: Recursion help

 
0
  #2
Dec 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Recursion help

 
0
  #3
Dec 4th, 2008
make stack profile
Last edited by quuba; Dec 4th, 2008 at 8:41 pm. Reason: stupid first reply
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz

Re: Recursion help

 
0
  #4
Dec 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 22
Reputation: pocku is an unknown quantity at this point 
Solved Threads: 0
pocku pocku is offline Offline
Newbie Poster

Re: Recursion help

 
0
  #5
Dec 13th, 2008
Alright, thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Recursion help

 
0
  #6
Dec 14th, 2008
> 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

Re: Recursion help

 
0
  #7
Dec 15th, 2008
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).
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC