0

Hello I am currently working on this code and the code can be runned well from 100-10,000 random elements after that the heap sort says stack over flow, im thinking the recursive calls are the cause of this, so please healp

```
public static void MAX_HEAPIFY(int [] A, int i, int n)
{
int l = i + 1;//sets the left tree root
int r = i + 2;//sets the right tree root
int largest = 0;
if(l <= n && A[l] > A[i])
{
largest = l;
}
else
{
largest = i;
}
if(r <= n && A[r] > A[largest])
{
largest = r;
}
if(largest != i)
{
int temp = A[largest];
A[largest] = A[i];
A[i] = temp;
MAX_HEAPIFY(A, largest, n);
}
}
//this method builds a max heap takes in an array and a int n
public static void BUILD_MAX_HEAP(int [] A, int n)
{
for(int i = (int) Math.floor(n/2); i >= 0; i --)
{
//calls the method max_heapify and inputs the Array passed in
//the i from the for loop
//and the n passed in as well.
MAX_HEAPIFY(A, i, n);
}
}
//this method is the heap sort it takes in one array and a lenght
public static void HEAPSORT(int [] A, int n)
{
BUILD_MAX_HEAP(A, n);
for(int i = n; i >= 1; i --)
{
//exchanges the elements of the arrays.
int temp = A[0];
A[0] = A[i];
A[i] = temp;
MAX_HEAPIFY(A, 0, i-1);
}
}
```

and here is the error

```
Exception in thread "main" java.lang.StackOverflowError
at AllAlgorithms.MAX_HEAPIFY(AllAlgorithms.java:24)
at AllAlgorithms.MAX_HEAPIFY(AllAlgorithms.java:45)
at AllAlgorithms.MAX_HEAPIFY(AllAlgorithms.java:45)
```

*Edited 4 Years Ago by fonzi*