【发布时间】:2017-03-31 18:45:43
【问题描述】:
#include <stdio.h>
#define ARRAY_SIZE 10
void lomuto (int A[], int l, int r, int smallerAtLeft)
{
if (smallerAtLeft == 1) //move elements smaller than pivot to the left and the greater ones to the right
{
int tmp, tmp2,pivot,i,j;
pivot = A[r];
i = l-1;
for (j =0; j<r-1; j++)
{
if (A[j] <= pivot)
{
i++;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
tmp2 = A[i+1];
A[i+1] = A[r];
A[r] = tmp2;
}
if (smallerAtLeft == 0) //move elements smaller than pivot to the right and the greater ones to the left
{
int tmp3, tmp4,pivot,i,j;
pivot = A[r];
i = l-1;
for (j=0; j<r-1; j++)
{
if (A[j]>= pivot)
{
i++;
tmp3 = A[i];
A[i] = A[j];
A[j] = tmp3;
}
}
tmp4 = A[i+1];
A[i+1] = A[r];
A[r] = tmp4;
}
}
void quicksort (int A[], int l, int r, int ascending)
{
lomuto (A,l,r,ascending);
}
int main()
{
int testarray;
int testArray[ARRAY_SIZE] = {4, 2, 5, 3, 6, 7, 8, 1, 0};
quicksort (testarray,0,8,1);
return testarray;
}
晚上好。 通常,我几乎在每个论坛和最深的线程中搜索我的代码中的可疑之处。 但是这次我没有找到可以帮助我的答案。如果有人能告诉我为什么 code-exe 停止工作但在编译过程中屏幕上没有显示错误,我将非常感激。 我们必须使用 lomuto 分区来实现快速排序算法。如果变量“smallerAtLeft”等于 1,则数组应按递增属性排序,如果等于 0,则应按递减排序。
此外,我们必须实现 void 函数,就像您在代码中看到的那样。包含 lomuto 的“lomuto-fct”和“quicksort-fct”。
也许这个 Reverse-Lomuto-Thread 将来也会帮助其他人..
【问题讨论】:
-
您有 2 个拼写不同的变量。您传递了错误的 1。这应该会产生编译器错误。
-
你展示的代码有什么问题?你想问什么问题?请花一些时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请read about how to ask good questions.
-
@JohnnyMopp 你的意思是什么变量? i 和 j?
-
在
main中有testarray和testArray -
@Someprogrammerdude “进程在 2.781 秒后退出,返回值 3221225477”是尝试编译代码并获得“task4.exe 不再工作”后的控制台输出 - 错误。由于我是编程新手(现在已经两周了),而且以前从未有人问过这种关于 Lomuto 反转的特殊问题,我想问一下是否有经验的程序员可以帮助我实现代码运行。 ..
标签: c error-handling quicksort