对于此选择排序,当我的数组中从未使用 0 时,为什么第一个数字是 0? (在c中)

发布于 2024-12-11 14:49:19 字数 2161 浏览 0 评论 0原文

这是 main 调用的代码,用于在 C 语言中使用选择排序对数组进行排序。我在 main 中打开了一个文件,并将前 10 个整数放入数组中,将第 11 个整数放入变量中,然后调用一堆简单的函数。整个事情重复了三遍。对于我的测试文件,最后两次迭代具有正确的打印排序,但第一次以 0 开头,但我的数组中没有 0。它还会删除最后一个 int。

预先感谢您的所有帮助!

这是我的代码:

    void sortme (int arry [], int last_int)
{
     int temp;
     int smallest_int;
     int current_int;
     int target_searcher;
     int numPrinted;

     for(current_int = 0; current_int < last_int; current_int++)
        {
            smallest_int = current_int;

            for(target_searcher = current_int + 1; target_searcher <= last_int; target_searcher++)
                if(arry[target_searcher] < arry[smallest_int])
                    smallest_int = target_searcher;

            temp = arry[current_int];
            arry[current_int] = arry[smallest_int];
            arry[smallest_int] = temp;
        }                                                                       //end outter loop

     numPrinted = 0;

     printf("\nThe sorted array is: ");
     for(current_int = 0; current_int < SIZE; current_int++)
        {
            printf("%4d", arry[current_int]);
            if(numPrinted < COUNT)
                numPrinted++;
            else
                {
                    printf("\n");
                    numPrinted = 0;
                }
        }
     printf("\n");

     return;
}

这是我的输出供参考(大部分内容都在 main.c 中注释掉):

The file opened.

Scanned into a[] and target is  33

ARRAY[1]
The contents in the array are:   40  32  57  27  67   6   3  89   2  99
The sorted array is:    0   2   3   6  27  32  40  57  67  89
The value searched,   33, was  not found.

Scanned into a[] and target is   3

ARRAY[2]
The contents in the array are:   86  43  89  32  45  12   1  58  98   4
The sorted array is:    1   4  12  32  43  45  58  86  89  98
The value searched,    3, was  not found.

Scanned into a[] and target is  11

ARRAY[3]
The contents in the array are:    1   2   3   4   5   6   7   8   9  10
The sorted array is:    1   2   3   4   5   6   7   8   9  10
The value searched,   11, was  not found.
Closing the file.
The file closed.

This is code called by main to sort an array using selections sort in the C language. I have a file opened in main and put the first ten ints into an array and the 11th into a variable and then a bunch of simple functions are called. The whole thing is repeated three times. For my test file, that last two iterations have the correct printed sort, but the first starts with 0, but I don't have a 0 in my array. It also drops the last int.

Thanks in advance for all your help!!

Here is my code:

    void sortme (int arry [], int last_int)
{
     int temp;
     int smallest_int;
     int current_int;
     int target_searcher;
     int numPrinted;

     for(current_int = 0; current_int < last_int; current_int++)
        {
            smallest_int = current_int;

            for(target_searcher = current_int + 1; target_searcher <= last_int; target_searcher++)
                if(arry[target_searcher] < arry[smallest_int])
                    smallest_int = target_searcher;

            temp = arry[current_int];
            arry[current_int] = arry[smallest_int];
            arry[smallest_int] = temp;
        }                                                                       //end outter loop

     numPrinted = 0;

     printf("\nThe sorted array is: ");
     for(current_int = 0; current_int < SIZE; current_int++)
        {
            printf("%4d", arry[current_int]);
            if(numPrinted < COUNT)
                numPrinted++;
            else
                {
                    printf("\n");
                    numPrinted = 0;
                }
        }
     printf("\n");

     return;
}

Here is my output for reference (most of the stuff is commenetd out in main.c):

The file opened.

Scanned into a[] and target is  33

ARRAY[1]
The contents in the array are:   40  32  57  27  67   6   3  89   2  99
The sorted array is:    0   2   3   6  27  32  40  57  67  89
The value searched,   33, was  not found.

Scanned into a[] and target is   3

ARRAY[2]
The contents in the array are:   86  43  89  32  45  12   1  58  98   4
The sorted array is:    1   4  12  32  43  45  58  86  89  98
The value searched,    3, was  not found.

Scanned into a[] and target is  11

ARRAY[3]
The contents in the array are:    1   2   3   4   5   6   7   8   9  10
The sorted array is:    1   2   3   4   5   6   7   8   9  10
The value searched,   11, was  not found.
Closing the file.
The file closed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-12-18 14:49:19

在搜索最小值时,您允许 target_searcher 等于 last_int。因此,有时您会在数组中注入一个随机的小值(并弄乱不属于您的内存)。当然,我假设 last_int 是数组的长度。

当您处理“仅有效索引”时,范围是从 0len-1。您可以通过长度为 1 的数组看到这一点(以防您再次有疑问)。由于只有 1 个元素,因此它位于 array[0]array[len-1] 处。

话虽如此,通常习惯以数组和长度的形式传递参数,而不是数组和最后一个有效元素的索引。这是更自然的。假设您有一个包含 len1len2 两个块的大型数组,以及一个对这些分区执行某些操作的函数。如果您使用长度作为参数,则可以使用:

processBlock(arr, len1);
processBlock(arr + len1, len2);

如果您要使用最后一个有效索引,则您必须处理所有这些 +/-1 术语。所以它是:

processBlockIdx(arr, len1 - 1);
processBlockIdx(arr + len1, len2 - 1);

或:

processBlockIdx(arr, lastIdx1);
processBlockIdx(arr + lastIdx1 +1, lastIdx2 - lastIdx1 - 1);

至于第二个问题的答案:是的,问题是由于访问数组边界之外的元素引起的。由于 C 没有检查数组边界的安全网,因此这样的错误通常表现为结果中出现无法解释的值,或更糟糕的是应用程序崩溃。在某些情况下,您没有那么幸运,它会在程序中完全不相关的部分体现问题。因此,最好非常了解数组元素的访问。

You allow target_searcher to be equal to last_int while searching for the minimum. So, sometimes you get a random small value injected into your array (and mess with memory that doesn't belong to you). Of course, I am assuming last_int is the length of the array.

When you are dealing with "valid indexes only", the range is from 0 to len-1. You can see that with an array of length 1 (in case you are ever in doubt again). Since there is only 1 element, it is at array[0], or array[len-1].

Having said that, it is usually customary to pass parameters as array and length instead of array and index of last valid element. It is more natural. Say, you have a large array with two blocks of len1 and len2, and a function that does something with these partitions. If you use length as parameter, you use:

processBlock(arr, len1);
processBlock(arr + len1, len2);

If you were to use last valid index, there would be all these +/-1 terms you'd have to deal with. So it is either:

processBlockIdx(arr, len1 - 1);
processBlockIdx(arr + len1, len2 - 1);

or:

processBlockIdx(arr, lastIdx1);
processBlockIdx(arr + lastIdx1 +1, lastIdx2 - lastIdx1 - 1);

As for the answer of your second question: yes, the problem was caused by accessing an element that is outside the bounds of your array. Since C doesn't have the safety net of checking array bounds, an error like this usually manifests itself as an unexplained value appearing in your results, or worse, an application crash. In some cases you are not as lucky, and it manifests the problem in a completely unrelated part of your program. So, it is best to be very sure about array element accesses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文