When I run the following code on a dataset of 500000 items I get a StackOverflowError. I've increased the JVM's available heap space to 512mb however I still get the same problem, I was wondering if anyone could tell me why?
This is the code i'm using:
package sorting2011; import java.util.Calendar; public class QuickSort implements Sorter{ @Override public void sort(Comparable[] items, int cutoff) { long startTime = Calendar.getInstance().getTimeInMillis(); quickSort(items, 0, items.length - 1); long endTime = Calendar.getInstance().getTimeInMillis(); long exeTime = endTime - startTime; System.out.println("Sort time: " + exeTime + " milliseconds"); } public void quickSort(Comparable array[], int left, int right){ int i = left; int k = right; if (right - left >= 1){ Comparable pivot = array[left]; Comparable tmp; while (k > i){ while (array[i].compareTo(pivot) != 1 && i <= right && k > i) //Stack trace says error here i++; while (array[k].compareTo(pivot) == 1 && k >= left && k >= i) k--; if (k > i){ tmp = array[i]; array[i] = array[k]; array[k] = tmp; } } tmp = array[left]; array[left] = array[k]; array[k] = tmp; quickSort(array, left, k - 1); //Stack trace says error here quickSort(array, k + 1, right); }else{ return; } } }
I have indicated in the code where the stack trace says the error is coming from.
Thanks in advance.