#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,则数组应按递增属性排序,如果变量“smallerAtLeft”等于 0,则应按递减属性排序。
此外,我们必须实现 void 函数,就像您在代码中看到的那样。 “lomuto-fct”和包含 lomuto 的“quicksort-fct”。
也许这个 Reverse-Lomuto-Thread 将来也会帮助其他人..
最佳答案
我认为您不明白 main
的返回值是什么是什么以及它的用途。它通常是成功和失败的指标,典型值0
成功时为小正值,失败时为小正值。甚至在 <stdlib.h>
中为此目的定义了宏。头文件: EXIT_SUCCESS
and EXIT_FAILURE
.
如果您想查看排序后的数组,您需要打印它:
printf("Sorted array = {");
for (unsigned i = 0; i < ARRAY_SIZE; ++i)
{
printf(" %d", testArray[i]);
}
printf(" }\n");
这当然需要您将实际的数组传递给排序函数。
关于C-Lomuto Quicksort Exe 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43147644/