【问题标题】:Why is this code for Selection Sort Algorithm not giving output?为什么选择排序算法的这段代码没有给出输出?
【发布时间】:2022-01-14 21:06:38
【问题描述】:

代码:

#include <stdio.h>

int main() {
    int i, j, temp, a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }, n = 10;

    printf("Before sorting, the array is:");

    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    for (i = 0; i < n - 1; i++) {
        temp = i;

        for (j = i + 1; j < n; j++) {
            if (a[j] < a[temp])
                temp = j;
        }

        if (temp != i) {//for swapping
            a[j] = a[j] + a[temp];
            a[temp] = a[j] - a[temp];
            a[j] = a[j] - a[temp];
        }
    }

    printf("\nAfter sorting, the array is:");

    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}

输出:

排序后的值没有被打印。这段代码的错误在哪里?

【问题讨论】:

  • 无需发布程序输出的文本图像。请将文本内嵌到您的帖子中。

标签: c algorithm sorting selection-sort


【解决方案1】:

您列出的代码产生的输出是:

Before sorting, the array is:9 8 7 6 5 4 3 2 1 0 
After sorting, the array is:9 8 7 6 5 4 3 2 0 4198464 

链接:

https://godbolt.org/z/o3nfz9Yv3

最后的垃圾似乎是这段代码的结果:

        if(temp!=i)
        {//for swapping
            a[j]=a[j]+a[temp];
            a[temp]=a[j]-a[temp];
            a[j]=a[j]-a[temp];
        }

当此代码执行时,j 始终等于 n,这意味着您始终在访问 a[j] 上的无效数据。我认为您的意思是使用 'i' 代替 'j'。

【讨论】:

    【解决方案2】:

    输出没有出现,因为您没有在数字后打印换行符。有些系统需要这个才能正确输出。

    但请注意,交换代码不正确:您应该交换 a[temp]a[i],而不是 a[j]

    通过加减交换值是不正确的:它不适用于浮点值和非数字类型,并且在溢出的情况下对于有符号整数具有未定义的行为。你应该使用一个临时变量:

    #include <stdio.h>
    
    int main() {
        int i, j, temp;
        int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
        int n = sizeof(a) / sizeof(a[0]);
    
        printf("Before sorting, the array is:");
        for (i = 0; i < n; i++) {
            printf(" %d", a[i]);
        }
        printf("\n");
    
        for (i = 0; i < n - 1; i++) {
            temp = i;
            for (j = i + 1; j < n; j++) {
                if (a[j] < a[temp])
                    temp = j;
            }
            if (temp != i) {//for swapping
                int t = a[temp];
                a[temp] = a[i];
                a[i] = t;
            }
        }
        printf("After sorting, the array is:");
        for (i = 0; i < n; i++) {
            printf(" %d", a[i]);
        }
        printf("\n");
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      看来,您有一个简单的错字:您应该在 for swapping 片段中使用 i 索引而不是 j

      当前代码有a[j],这是不正确,因为j == n 并且您尝试使用0 .. n - 1 out 的项目进行操作范围

          if (temp != i) {//for swapping
              a[j] = a[j] + a[temp];    // <- a[j] is in fact a[n] here
              a[temp] = a[j] - a[temp];
              a[j] = a[j] - a[temp];
          }
      

      应该是a[i],我们交换当前的a[i],找到a[temp]的物品:

          if (temp != i) {//for swapping
              a[i] = a[i] + a[temp];
              a[temp] = a[i] - a[temp];
              a[i] = a[i] - a[temp];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        • 2016-04-20
        • 1970-01-01
        • 2022-06-13
        • 2023-02-05
        • 2021-02-24
        相关资源
        最近更新 更多