Why am I getting a StackOverflowError?

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

Why am I getting a StackOverflowError?

 
0
  #1
Nov 29th, 2008
I am trying to create a heap sort method using recursion but whenever one of my methods called maxHeapify is called, it would throw a StackOverflowError. I understand that this error will pop up whenever my application recurses too deeply but the problem is I don't see anything wrong with my maxHeapify method that would cause such an error.

Here's the code:

  1. public class Sorting {
  2.  
  3. public static void maxHeapify (int[] A, int i) throws OutOfRangeException{
  4.  
  5. int heapSize = A.length;
  6.  
  7. if ((i < 0) || (i >= heapSize)){
  8. throw new OutOfRangeException();
  9. }
  10.  
  11. int left = (2*i)+1;
  12. int right = (2*i)+2;
  13. int largest = i;
  14.  
  15. if ((left < heapSize ) && (A > A[i])){
  16. largest = left;
  17. }
  18.  
  19. if ((right < heapSize) && (A > A[i])){
  20. largest = right;
  21. }
  22.  
  23. if (largest != i){
  24. int tmp = A[i];
  25. A[i] = A[largest];
  26. A[largest] = tmp;
  27. }
  28.  
  29. maxHeapify(A, largest);
  30. }
  31.  
  32.  
  33. public static void buildHeap (int[] A){
  34. int n = A.length;
  35. int i = (int) Math.floor(n/2) - 1;
  36.  
  37. try{
  38. while (i > 0){
  39. maxHeapify(A, i);
  40. i --;
  41. }
  42. }
  43. catch (OutOfRangeException ex){
  44. System.out.println("Error: Out of range");
  45. }
  46. }
  47.  
  48. public static int deleteMax (int[] A) throws Exception{
  49. int heapSize = A.length;
  50.  
  51. if (heapSize > 1){
  52. int largest = A[0];
  53. A[0] = A[heapSize -1];
  54. heapSize --;
  55. maxHeapify(A, 0);
  56. return largest;
  57. }
  58. else if (heapSize == 1){
  59. heapSize = 0;
  60. return A[0];
  61. }
  62. else{
  63. throw new Exception();
  64. //throw new EmptyHeapException();
  65. }
  66. }
  67.  
  68. public static void heapSort(int [] A){
  69.  
  70. int heapSize = A.length;
  71.  
  72. if (heapSize > 1){
  73. buildHeap(A);
  74. int i = heapSize-1;
  75.  
  76. while (i > 0){
  77. try{
  78. A[i] = deleteMax(A);
  79. i--;
  80. }
  81. catch (Exception ex){
  82. System.out.println("Heap is empty, unable to sort array.");
  83. }
  84. }
  85. }
  86. }
  87. }

It'll be great if anyone can help me out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,638
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Why am I getting a StackOverflowError?

 
0
  #2
Nov 29th, 2008
It doesn't seem like your maxHeapify method has a base case. Meaning, even when it finds the values you are looking for, it will keep looking for those values. You need a base case so that once the method finds what its looking for, it no longer continues calling itself. Try adding this:

  1.  
  2. if (largest != i){
  3. int tmp = A[i];
  4. A[i] = A[largest];
  5. A[largest] = tmp;
  6. }
  7. else{ //putting return here prevents the method from calling itself again
  8. return;
  9. }
Last edited by BestJewSinceJC; Nov 29th, 2008 at 10:29 pm.
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: Why am I getting a StackOverflowError?

 
0
  #3
Nov 29th, 2008
Ah, can't believe I missed that... Thanks for all your help. My code's running fine now.
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



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC