This code I have written works great until a certain input size. If the input gets too large, I get a "java.lang.StackOverflowError". I have read some other entries on stackoverflow regarding this topic and I think I got an error in my recursion- but I can't find it. Here is the code:
public int partition(int[] A, int l, int r, int x){
int i = l-1;
int j = r;
int exchange;
while(i <= j){
while(A[i] > x){
i++;
}
while(j >= i && A[j] <= x){
j--;
}
if(i < j){
exchange = A[i];
A[i] = A[j];
A[j] = exchange;
}
}
if(A[i] < A[r]){
exchange = A[i];
A[i] = A[r];
A[r] = exchange;
}
return i;
}
public void quicksort(int[] A, int l, int r){
int pivot = 0;
int x = 0;
if(l < r){
x = A[r];
pivot = partition(A, l, r, x);
quicksort(A, l, pivot-1);
quicksort(A, pivot+1, r);
}
}
java.lang.StackOverflowError
appears where you allocate too much into the memory 'stack' so you probably have an infinite loop somewhere. – Mr D May 11 '13 at 15:59