【问题标题】:Bubble Sort ending in an infinite loop冒泡排序以无限循环结束
【发布时间】:2016-12-09 13:37:09
【问题描述】:

我创建了一个冒泡排序程序。它最终陷入无限循环。 我在一些地方包含了 cmets,以便代码易于理解。 欢迎任何关于如何使代码更小的建议。

我在调试程序时发现了这个——

当stdin为“ccbbaa”时,经过一些递归,最终input(aabbcc)和temp(aabbcc)相同,然后strcmp()的条件执行后,'temp'的值变成了“baabcc” .

  1. 发生这种情况的任何原因? — 这就是无限循环的原因。

  2. 字符数组的末尾是否有“\0”(将输入复制到 temp 时)?

我通过使用 for 循环而不是 strcmp() 解决了这个问题。调查为什么 strcmp() 目前不起作用。

更新的代码可用 - http://ideone.com/4Bdblh(已解决)

错误代码

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

void sort(char* input)
{
    const int length = strlen(input);
    int j = length -1;
    char temp[length];
    for(int i=0; i<length; i++)
    {
        temp[i]= *(input+i);
    }

    while(j)
    {
        if((int)*(input+1) < (int)*(input))
        {
            char temp1;
            temp1 = *(input);
            *input = *(input + 1);
            *(input + 1) = temp1;
        }
        input++;
        j--;
    }
        input = input - length +1;
        while(strcmp(temp,input))
        {
        sort(input);
        }
}
int main()
{
    char* input = malloc(sizeof(char)*1000);
    scanf("%[^\n]%*c",input);
    sort(input);
    printf("%s",input);
    return 0;
}

【问题讨论】:

  • 你应该阅读How to Debug Small Programs。
  • 谢谢,正在阅读。会记住以后的帖子。
  • 你太复杂了......再次观看 CS50 的关于冒泡排序伪代码的视频,并尝试先使用简单的 char 数组来完成。没有指针算术或递归等。当您最终成功使其与数组和迭代一起使用时......然后您尝试递归,指针算术等
  • @PrakharSharma,我建议你暂时忘记const。它并不完全是你认为的意思,除非你很好地理解它,否则它对你没有帮助。我还建议您采纳 yLaguardia 的建议来简化。你让这变得比它需要的复杂得多,这让你感到困惑。您不需要递归或指针算术,甚至不需要strlen() 或strcmp()。虽然它可能不会伤害你,但你也不需要malloc()。

标签: c bubble-sort


【解决方案1】:

使用数组和for循环回答-

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 1000

void sort(char input[])
{
    const int length = strlen(input);
    int j = length -1;
    char temp[length];
    for(int i=0; i<length; i++)
    {
        temp[i]= input[i];
    }
    int l=0;
    while(j)
    {

        if(input[l+1] < input[l])
        {
            char temp1;
            temp1 = input[l];
            input[l] = input[l+1];
            input[l+1] = temp1;
        }
        l++;
        j--;
    }
        for(int k=0; k<length; k++)
        {
            if(temp[k]!=input[k])
            {
                sort(input);
            }
        }       
}
int main()
{
    char input[MAX];
    scanf("%[^\n]%*c",input);
    sort(input);
    printf("%s",input);
    return 0;
}

【讨论】:

    【解决方案2】:

    在 C 中,字符串只是一个以 0 结尾的字符数组。

    所有字符串函数都假定 char 数组以零结尾。

    strcpy 复制字符串,包括末尾的 0 分隔符。因此,destination 必须有足够的空间容纳字符串加零。

    strlen 返回字符串的长度,因此目的地必须至少为 strlen (input)+1 长。

    如果你在循环中复制字符串,那么你一定不要忘记添加结尾的零。

    我真正不明白的是为什么要使其递归并进行字符串比较以检测完成情况。您可以只实现两个从 0 到 length - 2 的嵌套循环。保证它最终会被排序。

    如果你想让它自适应,只需存储你最后交换的位置。下一个循环不需要再继续了。

    【讨论】:

      【解决方案3】:

      当stdin为“ccbbaa”时,经过一些递归,最终input(aabbcc)和temp(aabbcc)相同,然后strcmp()的条件执行后,'temp'的值变成了“baabcc” .

      1. 发生这种情况的任何原因? — 这就是无限循环的原因。

      看起来'temp' 的值已更改为“baabcc”,因为函数返回到本地temp 具有与以前相同的值的先前递归级别。 无限循环的主要原因是由于while(strcmp(temp,input)),旧的、未排序的temp被一次又一次地比较。

      1. 字符数组的末尾是否有“\0”(将输入复制到 temp 时)?

      错误代码中的temp 数组没有;你可以写

          char temp[length+1];
          strcpy(temp, input);
      

      或者只是使用strncmp() 而不是strcmp()。

      所以,要让程序正常工作,改变就足够了

              while(strcmp(temp,input))
      

      到

              if (strncmp(temp, input, length))
      

      【讨论】:

        猜你喜欢
        • 2012-09-17
        • 2017-06-29
        • 1970-01-01
        • 2016-08-31
        • 2018-05-05
        • 1970-01-01
        • 1970-01-01
        • 2020-07-22
        • 1970-01-01
        相关资源
        最近更新 更多