【问题标题】:Bubblesort ignores last element冒泡排序忽略最后一个元素
【发布时间】:2018-11-17 02:20:16
【问题描述】:

我正在尝试对指针数组进行排序,具体取决于它们指向的字符串。我的冒泡排序实现似乎忽略了我传递给它的最后一个元素。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void swap(char **a,char **b);
int main(void);

int main(void)
{
    char *ptr[1000]; //build an array of 1000 pointers
    short ptrpos = 0; //start at 0th pointer
    char input[500]; 
    printf("Enter strings(names), seperate by newline\nEOF(Ctrl-D) finishes the input process.\n");
    while(fgets(input,sizeof(input),stdin))
    {
        ptr[ptrpos] = malloc(strlen(input)+1); 
        strcpy(ptr[ptrpos],input); 
        ptrpos++; 
    }
    short length = ptrpos-1;

//BEGIN BUBBLE SORT
    for(short h = 1; h < length; h++)
    {
        for(short i = 0;i < length - h; i++)
        {
            if(strcmp(ptr[i],ptr[i+1]) > 0) 
                swap(&ptr[i],&ptr[i+1]); 
        }
    }
//END BUBBLE SORT
    printf("\n----- Sorted List -----\n");
    for(ptrpos = 0;ptrpos <= length;ptrpos++)
        printf("%s",ptr[ptrpos]);

    return 0;
}
void swap(char **a,char **b) //swaps adresses of passed pointers
{
    char *temp = *a;
    *a = *b;
    *b = temp;
}

输出如下:

输入字符串(名称),用换行符分隔
EOF(Ctrl-D) 完成输入过程。
回声
查理
狐步舞
Α
高尔夫球
布拉沃
三角洲

----- 排序列表 -----
Α
布拉沃
查理
回声
狐步舞
高尔夫球
三角洲

为什么最后一个字符串被忽略?我错过了什么明显的东西吗?

【问题讨论】:

    标签: c sorting bubble-sort


    【解决方案1】:

    数字只是例子。

    ptrpos0 开始计数,这意味着如果您有 6 个元素,则在您的 while 循环的最后一次迭代之后,ptrpos6。当你用

    计算长度时
    short length = ptrpos-1;
    

    你会得到length = 5

    您的for-loops 以counter &lt; length 结束,这意味着它们只计数到 4,这会产生 5 个元素而不是 6。

    由于数组的实际长度是6,我建议你把上面提到的那行改成

    short length = ptrpos;
    

    现在length 将等于数组中的元素数。

    【讨论】:

      【解决方案2】:

      这是导致问题的原因:

      short length = ptrpos-1;
      
      //BEGIN BUBBLE SORT
      for(short h = 1; h < length; h++)
      

      将循环更改为

      for(short h = 1; h <= length; h++)
      

      或者改变 for(ptrpos = 0;ptrpos &lt;= length;ptrpos++)
      for(ptrpos = 0;ptrpos &lt; length;ptrpos++) 还有,short length = ptrpos;

      到目前为止,用于排序的循环的执行时间比所需的时间少一倍。但是,打印的循环执行了 for(ptrpos = 0;ptrpos &lt;= length;ptrpos++) 的预期次数。

      我要做的更多改进:

      • 检查malloc是否返回NULL,然后只做进一步的访问。

      【讨论】:

      • 替代解决方案for(ptrpos = 0;ptrpos &lt; length;ptrpos++)short length = ptrpos; 更胜一筹。将 length 初始化为不是数组长度的东西会令人困惑且容易出错,在此循环中使用 &lt;= 运算符会进一步增加混乱。
      • @chqrlie:确实如此。
      【解决方案3】:

      这是一个工作版本, 我评论了我的修改

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      void swap(char **a,char **b);
      int main(void);
      
      int main(void)
      {
          char *ptr[1000]; //build an array of 1000 pointers
          short ptrpos = 0; //start at 0th pointer
          char input[500]; 
          printf("Enter strings(names), seperate by newline\nEOF(Ctrl-D) finishes the input process.\n");
          while(fgets(input,sizeof(input),stdin))
          {
              ptr[ptrpos] = malloc(strlen(input)+1); 
              strcpy(ptr[ptrpos],input); 
              ptrpos++; 
          }
          short length = ptrpos; //removed -1
      
      //BEGIN BUBBLE SORT
          for(short h = 1; h < length; h++)
          {
              for(short i = 0;i < length - h; i++)
              {
                  if(strcmp(ptr[i],ptr[i+1]) > 0)
                      swap(&ptr[i],&ptr[i+1]); 
              }
          }
      //END BUBBLE SORT
          printf("\n----- Sorted List -----\n");
          for(ptrpos = 0;ptrpos < length;ptrpos++) // transofrmed <= in <
              printf("%s",ptr[ptrpos]);
      
          return 0;
      }
      void swap(char **a,char **b) //swaps adresses of passed pointers
      {
          char *temp = *a;
          *a = *b;
          *b = temp;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        • 1970-01-01
        • 2016-04-13
        • 2020-10-29
        • 2016-12-18
        • 1970-01-01
        相关资源
        最近更新 更多