快速排序:莫名其妙,随机错误

#include <stdio.h>
#include <stdlib.h>
int numbers[101],size,i;
void C_sort(int L,int R){
    int L_arrow,R_arrow,box,temp;
    if(L>R){
        return;
    }
    box=numbers[L];
    L_arrow=L;R_arrow=R;
    printf("R_arrow=%d\n",R_arrow);
    for(i=1;i<=size-1;i++){
        if(i==L_arrow)printf("(%d) ",numbers[i]);
        else if(i==R_arrow)printf("[%d] ",numbers[i]);
        else printf("%d ",numbers[i]);
    }
    for(i=0;i<=size-1;i++)printf("%d ",numbers[i]);
    printf("\n");
    while(L_arrow!=R_arrow){
        while(numbers[R_arrow]>=box&&L_arrow<R_arrow){
            R_arrow--;
            printf("R_arrow=%d\n",R_arrow);
            for(i=0;i<=size-1;i++)printf("%d ",numbers[i]);
        }
        while(numbers[L_arrow]<=box&&L_arrow<R_arrow){
            L_arrow++;
            for(i=0;i<=size-1;i++)printf("%d ",numbers[i]);
        }
        if(L_arrow<R_arrow){
            temp=numbers[R_arrow];
            numbers[R_arrow]=numbers[L_arrow];
            numbers[L_arrow]=temp;
            for(i=0;i<=size-1;i++)printf("%d ",numbers[i]);
        }
        /*for(i=1;i<=size-1;i++){
        if(i==L_arrow)printf("(%d) ",numbers[i]);
        else if(i==R_arrow)printf("[%d] ",numbers[i]);
        else printf("%d ",numbers[i]);
        }*/
    //printf("\n");
    }
    numbers[L]=numbers[L_arrow];
    numbers[L_arrow]=box;
    for(i=1;i<=size-1;i++)printf("%d ",numbers[i]);
    printf("\n");
    C_sort(L,L_arrow-1);
    C_sort(L_arrow+1,R);
}
int main()
{
    int i;
    scanf("%d",&size);
    for(i=0;i<=size-1;i++)scanf("%d",&numbers[i]);
    //for(i=0;i<=size-1;i++)printf("%d ",numbers[i]);//测试代码,此时输出已不正常
    C_sort(0,size-1);
    for(i=1;i<=size-1;i++)printf("%d ",numbers[i]);
    system("pause");
    return 0;
}

有一些是测试代码显示过程。编译通过,然后结果明显错误,从过程来看也没有R_arrow的踪迹。Why?错误太随机了!!

bigmathbase
浏览 1271回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP