Thread: Recursion help
View Single Post
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