C-Lomuto Quicksort Exe不起作用

时间:2017-03-31 18:45:44

标签: c error-handling quicksort

#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-partitioning实现quicksort算法。如果变量“smallerAtLeft”等于1,则数组应按递增属性排序,如果等于0则递减。

此外,我们必须实现像您在代码中看到的void函数。 “lomuto-fct”和包含lomuto one的“quicksort-fct”。

也许这个Reverse-Lomuto-Thread将来也会帮助其他人..

1 个答案:

答案 0 :(得分:0)

我认为您不了解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");

当然要求您将实际数组传递给排序函数。