gpt4 book ai didi

c - 11/12 测试用例,带辅助函数的快速排序

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:00 25 4
gpt4 key购买 nike

这是没有通过的测试用例:

5 0 1 8 7

这是我的代码。

void swap(int* A, int* B)
{
int t;
t = *A;
*A = *B;
*B=t;
}

void sorthelper(int * arr,int ind1, int ind2)
{
int r = ind1+1;
int w = ind1+1;
// int i = 0;

if (ind2 - ind1 <= 1)
{
return;
}

for(r=ind1+1; r<=ind2;r++)//For r starting at one bigger then pivot and less then the length(ind2), increment r by one each time
{
if(arr[r] < arr[ind1])// if read is bigger then pivot, increment w and swap w and r
{
swap(&arr[w],&arr[r]);
w++;
}
}
swap(&arr[ind1], &arr[w-1]);//swap pivot with one less then write spot


sorthelper(arr, ind1, w-1);
sorthelper(arr, w ,ind2);

}


void sort(int * arr, int length)
{
int ind1 = 0;
int ind2 = 0;
ind2 = length-1;
sorthelper(arr, ind1, ind2);
return;
}

我正在尝试编写一个快速排序算法(是的,这是硬件),除了这个测试用例之外,我已经完成了所有工作。几个小时以来,我一直在尝试解决这个错误,但我失败了。我曾尝试使用 GDB 来跟踪我的值,但没有成功确定此错误。谁能提供任何意见?

首先运行排序函数,然后递归搜索助手并利用交换函数。

最佳答案

提示:当运行你的代码时,你会从递归调用 sorthelper 的第二行到达这样的调用。

sorthelper([0, 1, 5, 8, 7], 3, 4)

虽然它应该对 8 和 7 进行排序,但它什么也没做。想想为什么并修复它;)

关于c - 11/12 测试用例,带辅助函数的快速排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795807/

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