| | |
Why am I getting a StackOverflowError?
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 22
Reputation:
Solved Threads: 0
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:
It'll be great if anyone can help me out.
Here's the code:
Java Syntax (Toggle Plain Text)
public class Sorting { public static void maxHeapify (int[] A, int i) throws OutOfRangeException{ int heapSize = A.length; if ((i < 0) || (i >= heapSize)){ throw new OutOfRangeException(); } int left = (2*i)+1; int right = (2*i)+2; int largest = i; if ((left < heapSize ) && (A > A[i])){ largest = left; } if ((right < heapSize) && (A > A[i])){ largest = right; } if (largest != i){ int tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; } maxHeapify(A, largest); } public static void buildHeap (int[] A){ int n = A.length; int i = (int) Math.floor(n/2) - 1; try{ while (i > 0){ maxHeapify(A, i); i --; } } catch (OutOfRangeException ex){ System.out.println("Error: Out of range"); } } public static int deleteMax (int[] A) throws Exception{ int heapSize = A.length; if (heapSize > 1){ int largest = A[0]; A[0] = A[heapSize -1]; heapSize --; maxHeapify(A, 0); return largest; } else if (heapSize == 1){ heapSize = 0; return A[0]; } else{ throw new Exception(); //throw new EmptyHeapException(); } } public static void heapSort(int [] A){ int heapSize = A.length; if (heapSize > 1){ buildHeap(A); int i = heapSize-1; while (i > 0){ try{ A[i] = deleteMax(A); i--; } catch (Exception ex){ System.out.println("Heap is empty, unable to sort array."); } } } } }
It'll be great if anyone can help me out.
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
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:
Java Syntax (Toggle Plain Text)
if (largest != i){ int tmp = A[i]; A[i] = A[largest]; A[largest] = tmp; } else{ //putting return here prevents the method from calling itself again return; }
Last edited by BestJewSinceJC; Nov 29th, 2008 at 10:29 pm.
![]() |
Similar Threads
- StackOverflowError (Java)
- A doubt in SWING event handling (Java)
- help with recursive method (Java)
- URGENT: JSP - StackOverFlow Error (JSP)
Other Threads in the Java Forum
- Previous Thread: Problem reading data from file
- Next Thread: Stack class, Symbol Table help
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






