C数组排序后出现意外值 – 有人可以解释为什么吗?

huangapple go评论60阅读模式
英文:

Unexpected value present in C array after sorting - Can anyone explain why?

问题

这是在C语言中以升序对数组进行排序的一种简单方法,但在排序后,当我打印数组时,它变成了1 5 11 14 15 33。有人可以解释一下33是从哪里来的吗?因为我没有在数组中初始化33。

#include<stdio.h>

int main()
{
    int arr[6] = {11, 1, 5, 125, 15, 14};
    int i, j, k, temp;

    for (i = 0; i < 6; i++) {
        for (j = i + 1; j < 7; j++) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    for (k = 0; k < 6; k++) {
        printf("%d ", arr[k]);
    }
}
英文:

This is a simple way of sorting an array in ascending order in C. but after sorting when i print the array it comes as 1 5 11 14 15 33. Can someone explain me where the 33 comes from since i have not initialized 33 in my array

#include&lt;stdio.h&gt;

int main()
{
int arr [6]={11,1,5,125,15,14};
int i, j, k, temp;

for (i=0; i&lt;6; i++){
  for(j=i+1; j&lt;7; j++){
    if (arr[i]&gt;arr[j]){
        temp = arr[i];
        arr[i] = arr[j];
        arr[j]=temp;
    }
  }
}

for (k=0; k&lt;6; k++){
    printf(&quot;%d &quot;, arr[k]);
}
}

答案1

得分: 1

数组声明为 int arr[6] 时,其定义的索引范围是从 0 到 5,包括 0 和 5。循环 for(j=i+1; j&lt;7; j++) 允许 j 取值 6,而程序使用 j 来索引该数组。

因此,程序越界访问了 arr,而根据 C 标准,程序的行为未定义。

英文:

The defined indices of an array declared int arr[6] are 0 to 5, inclusive. The loop for(j=i+1; j&lt;7; j++) lets j go to 6, and the program uses j to index the array.

So the program accesses arr out of bounds, and the behavior of the program is not defined by the C standard.

答案2

得分: 0

你用来排序整数数组的算法叫做选择排序,通常需要两个for循环,其中内部for循环嵌套在外部for循环内,但外部循环从索引0开始,而内部循环从索引1开始,两个循环的最大长度必须相同,这意味着你的内部循环需要使用j<6而不是j<7,这是导致你的程序出现错误的原因,因为外部循环是i<6。内外循环的长度必须相同。请查看下面的代码,了解如何解决这个问题。

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

int main()
{
    int arr[6] = {11, 1, 5, 125, 15, 14};
    int i, j, k, temp;

    for (i = 0; i < 6; i++) {
        for (j = i + 1; j < 6; j++) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    for (k = 0; k < 6; k++) {
        printf("%d ", arr[k]);
    }
    // 输出 1 5 11 14 15 125
}
英文:

the algorithm you used to sort the integer array is called Selection Sort and it usually needs two for loops where the inner for loop is nested inside the outer for loop but the outer loop starts from index 0 while the inner loop starts from index 1, the maximum length for both loops must be the same, which means your inner loop needs to have j<6 instead of j<7 which is the one causing a bug in your program as the outer loop has i<6. The length needs to be the same for the inner and outer loop. Check the code below on how you can resolve that issue.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;


 int main()
    {
        int arr [6]={11,1,5,125,15,14};
        int i, j, k, temp;

        for (i=0; i&lt;6; i++){
            for(j=i+1; j&lt;6; j++){
                if (arr[i]&gt;arr[j]){
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j]=temp;
                }
            }
        }

        for (k=0; k&lt;6; k++){
            printf(&quot;%d &quot;, arr[k]);
        }
        //outputs 1 5 11 14 15 125 
    }

huangapple
  • 本文由 发表于 2023年5月28日 19:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定