I'm working on a quick sort algorithm using recursion but it's throwing a stackOverflowError. Here is the code:

```
public static void quicksort(int [] A){
quickSortRec(A, 0, A.length-1);
}
public static void quickSortRec(int [] A, int p, int r){
if (p < r){
int q = Partition(A, p, r);
quickSortRec(A, p, q-1);
quickSortRec(A, q+1, r);
}
}
public static int Partition(int[] A, int p, int r){
int x = A[r];
int i = p-1;
int j = p;
while (j < r){
if (A[j] <= x){
i++;
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
j++;
}
int tmp = A[i+1];
A[i+1] = A[r];
A[r] = tmp;
return i+1;
}
```

The thing that's puzzling me is there's no problems when I call for it from this piece of code:

```
public class A5Q5 {
public static void main(String[] args){
//Length of array is given by command argument
int length = Integer.parseInt(args[0]);
//100 arrays of variable lengths of random elements are created and then sorted
for (int i=0; i < 100; i++){
int[] arr = new int[length];
for (int j = 0; j < length; j++){
Random rand = new Random();
int n = rand.nextInt(65578);
arr[j] = n;
}
try{
Sorting.heapSort(arr);}
catch (EmptyHeapException ex){
System.out.println("Heap is empty, unable to sort array.");
}
catch (OutOfRangeException ex){
System.out.println("Error: Out of Range");
}
Sorting.insertionSort(arr);
Sorting.quicksort(arr);
Sorting.quicksortImproved(arr);
Arrays.sort(arr);
}
}
}
```

but a stackOverflowError is thrown when I try to call for it from this one:

```
public class A5Q6 {
public static void main(String[] args){
//Length of array is given by command argument
int length = Integer.parseInt(args[0]);
//100 arrays of variable lengths of elements of ascending values are created and then sorted
for (int i=0; i < 100; i++){
int[] arr = new int[length];
int num = 0;
for (int j = 0; j < length; j++){
Random rand = new Random();
int n = rand.nextInt(10);
num += n;
arr[j] = num;
}
try{
Sorting.heapSort(arr);}
catch (EmptyHeapException ex){
System.out.println("Heap is empty, unable to sort array.");
}
catch (OutOfRangeException ex){
System.out.println("Error: Index is out of range.");
}
Sorting.insertionSort(arr);
Sorting.quicksort(arr);
Sorting.quicksortImproved(arr);
Arrays.sort(arr);
}
}
}
```

Any help will be greatly appreciated.