I'm getting a StackOverFlowError when I try and use my recursive sort on an array that has a length that is greater than around 50. The method sorts properly if the array is small enough to not throw this error. Is there any way around this?
public class RecursiveSort
{
static double[] arr;
static int count = 0;
public static void main(String[] args)
{
double[] anArr = new double[50];
for(int i = 0; i < anArr.length; i++) {
anArr[i] = Math.random();
}
arr = anArr;
recurseSort(arr.length - 1);
display();
}
public static void recurseSort(int position)
{
if(position == 0)
{
position = arr.length - 1;
count++;
}
if (arr[position] < arr[position - 1])
{
double n = arr[position - 1];
arr[position - 1] = arr[position];
arr[position] = n;
}
if(count <= arr.length)
recurseSort(--position);
}
public static void display()
{
for(double n : arr)
System.out.println(n);
}
}