在我的代码中,该方法将调用自身(递归)。我们知道,深度方法调用会导致堆栈溢出。那么,当数据量很大时,会不会发生栈溢出呢?
我的快速排序代码:(排序类的内部类)
private static class QuickSortor extends Sortor {
protected <E> void sort(E[] a, boolean isAsc) {
quickSort(a, 0, a.length - 1, isAsc);
}
private <E> void quickSort(E[] a, int left, int right, boolean isAsc) {
if (left >= right)
return;
int middle = left;
Comparable<E> cmp = (Comparable<E>) a[left];
for (int i = left + 1; i <= right; i++) {
int result = cmp.compareTo(a[i]);
if (!isAsc)
result = -result;
if (result >= 0)
swap(a, ++middle, i);
}
swap(a, left, middle);
quickSort(a, left, middle - 1, isAsc);
quickSort(a, middle + 1, right, isAsc);
}
}