While implementing 3-way-quicksort I've encountered a error message that I don't know how to fix. This is the traceback for the error in console:
Exception in thread "main" java.lang.StackOverflowError
at projects.Main.quickSortByRecursion(Main.java:45)
at projects.Main.quickSortByRecursion(Main.java:47)
at projects.Main.quickSortByRecursion(Main.java:47)
at projects.Main.quickSortByRecursion(Main.java:47)
This goes for many more lines, 200 or so. Basically I'm just trying to implement a 3-way-quicksort. Here's my code:
static void partition(int[] intArray, int low, int high, int i, int j) {
if(high - low <= 1) {
if(intArray[high] < intArray[low]) {
swapElements(intArray, high, low);
}
} else {
i = low;
j = high;
return;
}
int midpoint = low;
int pivot = intArray[high];
while (midpoint <= high) {
if(intArray[midpoint] < pivot) {
swapElements(intArray, low + 1, midpoint + 1);
}
else if(intArray[midpoint] == pivot) {
midpoint += 1;
}
else if(intArray[midpoint] > pivot) {
swapElements(intArray, midpoint, high - 1);
}
i = low - 1;
j = midpoint;
}
}
static void quickSort(int intArray[]) {
quickSortByRecursion(intArray, 0, intArray.length - 1);
}
static void quickSortByRecursion(int intArray[], int low, int high) {
if(low >= high) {
return;
}
int i = 0;
int j = 0;
partition(intArray, low, high, i, j);
quickSortByRecursion(intArray, low, i);
quickSortByRecursion(intArray, j, high);
}
static void swapElements(int intArray[], int low, int high) {
int temporaryValue = intArray[low];
intArray[low] = intArray[high];
intArray[high] = temporaryValue;
}
public static void main(String[] args) {
int[] intArray = { 1, 3, 2, 4, 7, 9, 8, 5, 4, 6 };
for(int i : intArray) {
System.out.println(i);
}
quickSort(intArray);
for(int i: intArray) {
System.out.println(i);
}
}
}
I was also implementing other quicksorts, one by recursion second one with insertionSort inside and I haven't encountered with this issue yet. I've looked for similar answers but only thing I found was a incorrect usage of int/long parameters. Any ideas?
Java is pass by value. So changing i and j inside partition method will not be seen inside the caller method quickSortByRecursion.
Below is a workaround to fix the StackOverflowError error.
static void partition(int[] intArray, int low, int high, int[] arr) {
if (high - low <= 1) {
if (intArray[high] < intArray[low]) {
swapElements(intArray, high, low);
}
} else {
arr[0] = low;
arr[1] = high;
return;
}
int midpoint = low;
int pivot = intArray[high];
while (midpoint <= high) {
if (intArray[midpoint] < pivot) {
swapElements(intArray, low + 1, midpoint + 1);
} else if (intArray[midpoint] == pivot) {
midpoint += 1;
} else if (intArray[midpoint] > pivot) {
swapElements(intArray, midpoint, high - 1);
}
arr[0] = low - 1;
arr[1] = midpoint;
}
}
static void quickSort(int intArray[]) {
quickSortByRecursion(intArray, 0, intArray.length - 1);
}
static void quickSortByRecursion(int intArray[], int low, int high) {
if (low >= high) {
return;
}
int[] arr = {0, 0};
partition(intArray, low, high, arr);
quickSortByRecursion(intArray, low, arr[0]);
quickSortByRecursion(intArray, arr[1], high);
}
static void swapElements(int intArray[], int low, int high) {
int temporaryValue = intArray[low];
intArray[low] = intArray[high];
intArray[high] = temporaryValue;
}
public static void main(String[] args) {
int[] intArray = { 1, 3, 2, 4, 7, 9, 8, 5, 4, 6 };
for (int i : intArray) {
System.out.println(i);
}
quickSort(intArray);
for (int i : intArray) {
System.out.println(i);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.