繁体   English   中英

C:使用Bubblesort算法对2d数组进行排序只能单向进行

[英]C : Sorting a 2d array using bubblesort algorithm only works one-way

我知道有一些关于在C中将2d数组冒泡的文章,但我的问题有点奇怪,因为它仅在升序排序时发生,而降序排序就很好(而且这两种算法都是完全类比的)。升序排序仅打印每个“单词”的一半。

我的程序应该对以白色符号分隔的字符串进行冒泡排序,这些字符串可以从标准输入或文件中读取。输入标准输入时,只需输入一个点,然后从文件末尾读到EOF即可完成对标准输入的读取。

代码如下:

int main()
{ FILE *plik=NULL;
int i,j; 
int k,l; 
int m; 
int a,b; 
char tym; 
char odp; 
char ans;  
char c; 
char tab[100][100];     
char nazwa[100];

printf("Would you like to sort from a file?(Y/N)\n");
scanf("%s",&odp);
if(odp=='n' || odp=='N')
    {printf("Enter character strings and finish with a DOT:\n");
     getc(stdin);   
     i=0;
     j=0;
     while(1)
            {c=getc(stdin);//reading from stdin
            if(c!='\n' && c!='\t' &&c!=' ')
                {tab[i][j]=c;
                if(tab[0][j]=='.')  //ending input
                    {tab[0][j]='\0'; //clearing the extra sign
                     break;}
                 i++;   //columns counter
                 printf("%c",c);}
            else
                 {i=0;
                 j++; //row counter
                 printf("\n");}}}
else if(odp=='y' || odp=='Y')
    {printf ("Enter the file's name:\n");
     scanf("%99s",&nazwa); 
     plik = fopen(nazwa,"rt");
     if(plik==NULL)
        {printf("\nFailed reading from file : %s \n\n",nazwa);    //jesli blad odczytu przerwij dzialanie
         printf("Check the file and try again \n");
         fclose(plik);
         return 0;}
     else
        {i=0;
         j=0;
         do
            {c=getc(plik);
            if(c!='\n' && c!='\t' &&c!=' ')
                {tab[i][j]=c;
                 i++;   //columns counter
                 printf("%c",c);}
            else
                 {i=0;
                 j++; //rows counter
                 printf("\n");}}
         while(c!=EOF);} //ending reading
    } fclose(plik);

  //Here starts the sorting
    printf("\nSelect sorting order (A/D)\n");
    scanf("%s",&ans);
if(ans=='a' || ans=='A') //ascending
    {for(m=0;m<j;m++) //moving to next rows
        for(k=0;tab[k][m]!='\0';k++)//later as in a typical bubblesort
            for(l=0;l<100-1-k;l++)
                if(tab[l][m] > tab[l+1][m])
                    {tym = tab[l+1][m];
                    tab[l+1][m] = tab[l][m];
                    tab[l][m] = tym;}}

else if (ans=='D' || ans=='d') //descending
    {for(m=0;m<j;m++) //moving to next rows
        for(k=0;tab[k][m]!='\0';k++) //later as in typical bubblesort, analogically to ascending order
            for(l=0;l<100-1-k;l++)
                if(tab[l][m] < tab[l+1][m])
                    {tym = tab[l+1][m];
                    tab[l+1][m] = tab[l][m];
                    tab[l][m] = tym;};}

//returning results
a=0;
b=0;
while(b<j)
    {if (tab[a][b]!='\0')
        {putchar(tab[a][b]);
         a++;}
    else
        {a=0;
         b++;
         printf("\n");}

}
return 0;
 }

很抱歉我的英语和代码混乱,但是我仅在一个月前开始编程。 如果有人可以帮助我解决这个问题,我将非常感激。 感谢您的时间 :)

除了

发布的代码没有在tab [] []数据的每个字符串的末尾附加NUL字节('\\ 0'),也没有将tab [] []变量初始化为所有'\\ 0',

错误在

        for(k=0;tab[k][m]!='\0';k++)//later as in a typical bubblesort
            for(l=0;l<100-1-k;l++)

- 循环不考虑字符串的结尾,导致字符冒泡,超出字符串终止,并且在最终输出排序后的字符串时不再可见。 可以通过将内循环头更改为

            for (l=k; l--; )

因此仅在字符串范围内运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM