我正在实施一个快速排序,我收到stackoverflow错误。我已经尝试了多次,看到这个错误感到沮丧。任何人都可以帮助我们
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr = { 5, 7, 6, 1, 3, 2, 4 };
int lowerIndex = 0;
int higherIndex = arr.length - 1;
quickSort(arr, lowerIndex, higherIndex);
System.out.println(" FiNaLly " + Arrays.toString(arr));
}
private static void quickSort(int[] arr, int lowerIndex, int higherIndex) {
int pivot = arr[arr.length - 1];
int i = 0;
int temp = 0;
int middle = arr.length / 2;
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j] < pivot) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
if (i == middle) {
int swap = arr[middle];
arr[middle] = arr[arr.length - 1];
arr[arr.length - 1] = swap;
}
}
int q = Arrays.binarySearch(arr, pivot);
if (higherIndex > lowerIndex) {
quickSort(arr, 0, q - 1);
}
if(q<arr.length-1){
quickSort(arr, q+1, arr.length-1);
}
}
}