I've been working on this code for a few days with no luck because of a stack overflow error during recursion.
The problem states that our pivot is always the 0th element and through recursion we can sort an array by either swapping or creating new arrays and copy them into the original array. I chose to create 2 arrays since it is easier for me to understand but once I reach the recursive step i get this error.
I traced the algorithm and realized that the pivot element is always being placed in the lower array and may be the problem.
enter code here
public static void main(String[] args) {
int[] arr = new int[] { 12, 7, 5, 8, 4, 6, 11, 15 };
quicksort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
private static int[] quicksort(int[] arr, int middle) {
// Base case
if (arr.length == 1) {
return arr;
}
int[] upper = new int[arr.length];
int[] lower = new int[arr.length];
int upperIndex = 0, lowerIndex = 0;
// Put the elements into their respective pivots
for (int i = 0; i < arr.length; i++) {
if (arr[i] <= middle) {
lower[lowerIndex++] = arr[i];
} else {
upper[upperIndex++] = arr[i];
}
}
// Recurse, and sort the pivots
lower = quicksort(lower, lower[0]);
upper = quicksort(upper, upper[0]);
// Combine lower, middle, and upper back into one array
System.arraycopy(lower, 0, arr, 0, lowerIndex);
arr[lowerIndex + 1] = middle;
System.arraycopy(upper, 0, arr, lowerIndex + 2, upperIndex);
return arr;
}
The result should be a sorted array in ascending order.