gpt4 book ai didi

c++ - 未发现快速排序错误

转载 作者:行者123 更新时间:2023-11-30 01:30:31 25 4
gpt4 key购买 nike

在为此工作了一整天后,我找不到我的代码有什么问题,我找不到我的错误在哪里。有人可以帮我弄清楚我做错了什么吗?

void a_quick(int array[], int i, int j){
//Array Quick Sort
if ((j - i) < 2){
return;
}
int top = j;
int bot = i;
int p_location = i + (j + i) / 2;
int pivot = array[p_location];
while (i < j){
while (array[i] < pivot){
i++;
}
while (array[j] > pivot){
j--;
}
if (i <= j){
swap(array[i], array[j]);
i++;
j--;
}
}
a_quick(array, bot, p_location);
a_quick(array, p_location + 1, top);

}

在 64k int 数组上运行代码会使计算机崩溃,我需要让它在 128k 上运行。

我得到:

Test Array:
703 322 673 141 253 547 662 37 723 529 316 190 288 40 264 446 890 370 6 393

Quick Sort w Array:
6 37 40 141 253 190 316 288 264 322 393 370 446 529 723 662 890 547 673 703

最佳答案

这段代码对我来说很好用

   void a_quick(int array[], int i, int j){
//Array Quick Sort

int top = j;
int bot = i;
int p_location = (j + i) / 2;
int pivot = array[p_location];
do {
while (array[i] < pivot)i++;
while (array[j] > pivot)j--;
if (i <= j){
swap(array[i], array[j]);
i++;
j--;
}
} while (i<=j);
if (bot<j) a_quick(array, bot, j);// problem is most likely here
if (i<top) a_quick(array, i, top);// or here
}

关于c++ - 未发现快速排序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4340469/

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