| | |
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:
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,657
Reputation:
Solved Threads: 206
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
Views: 430 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






