简体   繁体   English

合并排序程序中的错误

[英]Error in merge sort program

I am doing a merge sort program in C but I'm getting some unexpected output. 我正在用C做一个合并排序程序,但是得到了一些意外的输出。 Can anyone find out error in the program? 任何人都可以找出程序中的错误吗?

   #include<stdio.h>

    int array[100],n;
    void merge(int l,int m,int h);
    void mergesort(int start,int end);
    main(){
    int i;
    printf("Enter Size: ");
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&array[i]);      
    }
    mergesort(0, n-1);
    for(i=0;i<n;i++){
        printf("%d\n",array[i]);        
        }
    }

    void mergesort(int start,int end){
    int mid;
    if(start<end){
        mid=(start+end)/2;
        mergesort(start,mid);
        mergesort(mid+1,end);
        merge(start,mid,end);
        }
     }

    void merge(int start,int mid,int end){
    int i,j,k;
    int a1[100],a2[100];

    for(k=0,i=start;i<=mid;i++,k++){
        a1[k]=array[i];
    }
    for(k=0,j=i;j<=end;j++,k++){
        a2[k]=array[j]; 
    }
    a1[mid]=999;
    a2[j]=999;
    i=0;j=0;
    for(k=start; k <=end; k++)
    {
        if(a1[i]<=a2[j])
            array[k]=a1[i++];
        else
            array[k]=a2[j++];
    }
}

Output: 输出:

Enter Size: 5
1 2 3 2 3
2
2
3
3
-1818025592

For larger arrays, the output is more scrambled. 对于更大的阵列,输出会更加混乱。 I think there is an error in merge() function. 我认为merge()函数中有错误。

You are terminating your a1 and a2 arrays at the wrong location. 您将a1和a2阵列终止在错误的位置。

Try changing: 尝试更改:

for(k=0,i=start;i<=mid;i++,k++){
    a1[k]=array[i];
}
for(k=0,j=i;j<=end;j++,k++){
    a2[k]=array[j]; 
}
a1[mid]=999;
a2[j]=999;

to

for(k=0,i=start;i<=mid;i++,k++){
    a1[k]=array[i];
}
a1[k]=999;
for(k=0,j=i;j<=end;j++,k++){
    a2[k]=array[j]; 
}
a2[k]=999;

When you create your temporary arrays: 创建临时数组时:

for (k = 0, i = start; i <= mid; i++, k++) {
    a1[k] = array[i];
}
a1[i] = 999;

you put a sentinel value with a high value at the end. 您将结尾值较高的哨兵值放在后面。 But i is the index into the original array . 但是i是原始array的索引。 You must use k , the index of the temporary array a1 : 您必须使用k ,即临时数组a1的索引:

for (k = 0, i = start; i <= mid; i++, k++) {
    a1[k] = array[i];
}
a1[k] = 999;

Even so, the double control structure with k and i is clumsy. 即使这样,具有ki的双重控制结构仍然笨拙。 And there's no need to guess a high number; 无需猜测太多; <limits.h> has constants defined for the min and max values of most types: <limits.h>具有为大多数类型的最小值和最大值定义的常量:

k = 0;
for (i = start; i <= mid; i++) {
    a1[k++] = array[i];
}
a1[k] = INT_MAX;

Here, you increment k when an item is appended. 在此,当附加项目时, k增加。 This works even when the assignment happens conditionally, ie not in all iterations. 即使赋值是有条件的,即不是在所有迭代中都可以进行,这仍然有效。

Finally, I would recommend to use exclusive upper bounds. 最后,我建议使用互斥上限。 That's the natural way to express ranges in C. mid would then be both the exclusive upper bound of the left array and the inclusive lower bound of the right array. 这是在C mid表达范围的自然方法。然后, mid既是左数组的排他上限,又是右数组的排他下限。

I have solved it 我已经解决了
a[mid + 1]=999 a [mid + 1] = 999
a[k]=999 a [k] = 999

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

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