用于快速排序(递归)的C程序

用于快速排序(递归)的C程序,c,recursion,quicksort,C,Recursion,Quicksort,下面的c程序是一个快速排序递归程序。虽然我已经根据Cormen的解释编写了这段代码,但是它对输入进行了错误的排序。例如,它对输入进行了3,8,1到3,1,8的排序。事先非常感谢您发现了错误 #include<stdio.h> void printa(int a[],int size) { int i; printf("\n"); for(i=0;i<size;i++) { printf("%d\n",a[i]); } }

下面的c程序是一个快速排序递归程序。虽然我已经根据Cormen的解释编写了这段代码,但是它对输入进行了错误的排序。例如,它对输入进行了3,8,1到3,1,8的排序。事先非常感谢您发现了错误

#include<stdio.h>
void printa(int a[],int size)
{
    int i;
    printf("\n");
    for(i=0;i<size;i++)
    {
        printf("%d\n",a[i]);
    }
}
void swap(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
int partition(int a[],int p,int r)
{
    int i,j,x;
    i=p-1;
    x=a[r];
    for(j=p;j<r;j++)
    {
        if(a[j]<=x)
        {
            i=i+1;
            swap(&a[i],&a[j]);
        }
        swap(&a[i+1],&a[r]);
    }
    return i+1;
}
void quicksort(int a[],int p,int r)
{
    if(p<r)
    {
    int q;
    q=partition(a,p,r);
    quicksort(a,p,q);
    quicksort(a,q+1,r);
    }
}
main()
{
    int a[50],i,size;
    printf("enter the size of the array\n");
    scanf("%d",&size);
    printf("enter the elements of the array\n");
    for(i=0;i<size;i++)
    {
        scanf("%d",&a[i]);
    }
    quicksort(a,0,size-1);
    printa(a,size);

}
#包括
无效打印a(整数a[],整数大小)
{
int i;
printf(“\n”);

对于(i=0;i一般建议:当您已经有一个固定大小的数组时,不要要求用户输入数组大小。它可能会出错。无条件的
swap
看起来可疑。您的测试用例足够小,您应该能够使用pen(cil)进行调试和纸。不,交换情况是有限的。它需要把支点放在两个子数组要被区分的中间,哦,得到它。无条件交换应该在for循环中。谢谢。