gpt4 book ai didi

java - 快速排序 - StackOverflowException

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:34:47 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个快速排序算法。请看下面的代码:

public static void quickSort(int[] tab, int lowIndex, int highIndex) {
if (tab.length == 0 || tab == null) {
return;
}

int i = lowIndex;
int j = highIndex;

int pivot = tab[tab.length / 2];

while (i <= j) {
while (tab[i] < pivot) {
i++;
}
while (tab[j] > pivot) {
j--;
}

if (i <= j) {
int c = tab[i];
tab[i] = tab[j];
tab[j] = c;
i++;
j--;
}

if (lowIndex < j) {
quickSort(tab, lowIndex, j); // error !!!
}
if (i < highIndex) {
quickSort(tab, i, highIndex);
}
}

}

问题是线程“主”java.lang.StackOverflowError 中的异常。怎么了?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com