简体   繁体   English

错误的合并排序结果

[英]wrong merge sort results

i wrote a regular merge sort array code and all i want to do is to call to this function with 'asize' and not with a number but instead of recieving [1 2 3 4 5 6 7 8 9 10] a regular sorted array i get [-858993460 1 2 3 4 5 6 7 8 9] 我写了一个常规的合并排序数组代码,我要做的就是用'asize'而不是一个数字来调用此函数,而不是接收[1 2 3 4 5 6 7 8 9 10]一个常规的排序数组i得到[-858993460 1 2 3 4 5 6 7 8 9]

please help me to find the reason for this 请帮助我找到原因

void merge_sort(int *a,int first, int last)
{
     int middle;
            if(first < last)
           {
                middle=(first+last)/2;
                merge_sort(a,first,middle);
                merge_sort(a,middle+1,last);
                merge(a,first,middle,last);
            }
}






void main()
{

    int a[] = {9, 7, 2, 3, 5, 4, 1, 8, 6, 10};
    int asize= (sizeof a / sizeof a[0]);
  merge_sort(a, 0, asize);
For (i = 0; i < 10; i++)
        printf ("%d ", a[i]);

Because your resultant array has one strange value and one missing value, the most likely case is an off-by-one error, likely to be passing asize as the index of the last element rather than asize - 1 . 因为结果数组具有一个奇怪的值和一个缺失的值,所以最有可能的情况是一个asize一的错误,很可能传递asize作为最后一个元素的索引,而不是asize - 1

The parameters needed by your merge sort are the first and last indexes (zero-based) so, for an array of size 10, that would be 0 and 9 . 合并排序所需的参数是第一个索引和最后一个索引(从零开始),因此,对于大小为10的数组,该09 You're passing 0 and 10 with your current code. 您正在使用当前代码传递010

Because of this, you're actually sorting the "array": 因此,您实际上是在对“数组”进行排序:

{9, 7, 2, 3, 5, 4, 1, 8, 6, 10, ?}
 \___________________________/  |
          your array            +-- not your array

and, because ? 并且,因为? is -858993460 (in this particular case), you end up with: -858993460 (在这种情况下),您最终得到:

{-858993460, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 \___________________________________/  \/ not your
              your array                 \  array

Then, printing the first ten elements gives you the output you're seeing. 然后,打印前十个元素将提供您所看到的输出。 Unfortunately, whatever variable (or stack control information or anything really) that was holding that large negative value has now been overwritten with the value ten, not something that you really want. 不幸的是,现在拥有大负值的任何变量(或堆栈控制信息或任何其他东西)现在都已被值10覆盖,而不是您真正想要的值。

What you're doing is undefined behaviour. 您正在做的事情是不确定的行为。 Stop it immediately. 立即停止。 Don't make me come over there :-) 不要让我过来:-)

The easy fix, by the way, is to change: 顺便说一下,简单的解决方法是更改​​:

merge_sort (a, 0, asize);

into: 变成:

merge_sort (a, 0, asize - 1);

You're stepping over the end of the array into garbage. 您正在将数组末尾移入垃圾箱。 last should be asize-1 , that is, call merge_sort(a, 0, asize-1); last应该是asize-1 ,即调用merge_sort(a, 0, asize-1); .

merge_sort(a, 0, asize);

You should send asize-1 ie from 0 to asize-1 elements. 您应该发送asize-1即从0asize-1元素。 The following is the correct one! 以下是正确的一个!

merge_sort(a, 0, asize-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM