0

我正在尝试实现 CLRS 算法书中提到的合并排序算法。我想出了以下代码。

#include<stdio.h>
#include<stdlib.h>
void merge_sort(int *arr,int start_index,int end_index);
void merge(int *arr,int start_index,int middle_index,int end_index);

int main(){

int arr[]={5,2,1,6,0,3,3,4}; //8 elements last index 7
int i;
printf("Before sorting.\n");
for(i=0;i<8;i++)
printf("%d,",arr[i]);
merge_sort(arr,0,7);
printf("\nAfter sorting.\n");
for(i=0;i<8;i++)
printf("%d,",arr[i]);

return 0;}

void merge_sort(int *arr,int start_index,int end_index){
    int middle_index;
    if(start_index<end_index)
    {
        middle_index=(start_index+end_index)/2;
        merge_sort(arr,start_index,middle_index);
        merge_sort(arr,(middle_index+1),end_index);
        merge(arr,start_index,middle_index,end_index);
    }

}

void merge(int *arr, int start_index,int middle_index, int end_index){

    int n1,n2,i,l,m;
    n1=middle_index-start_index+1;
    n2=end_index-middle_index;
    int sub_arr1[n1+1],sub_arr2[n2+1];
    for(i=0;i<=(n1-1);i++)
        sub_arr1[i]=arr[i];
     for(i=0;i<=(n2-1);i++)
        sub_arr2[i]=arr[middle_index+1+i];

    sub_arr1[n1]=100;
    sub_arr2[n2]=100;
        l=0,m=0;
    for(i=0;i<=end_index;i++){


        if(sub_arr1[l]<sub_arr2[m])
        {arr[i]=sub_arr1[l];
        l=l+1;}
        else
         {arr[i]=sub_arr2[m];
        m=m+1;}

    }}
There seems to be some error in the merge function due to which I am getting an erroneous output which is as follows.

Before sorting.
5,2,1,6,0,3,3,4,
After sorting.
2,4,100,-1076668400,2,4,100,7,
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

由于我用这种方法排序的数字很小,所以我使用 100 作为标记值。这可能是错误的可能来源吗?任何帮助表示赞赏。

4

1 回答 1

1

您的合并功能有一个错误:您忽略了 start_index。这些行:

for(i=0;i<=(n1-1);i++)
    sub_arr1[i]=arr[i];

应替换为:

for(i=0;i<=(n1-1);i++)
    sub_arr1[i]=arr[start_index+i];

而最后一个 for 循环应该以 i = start_index 开始:

for (i=start_index;i<=end_index;i++) { /* ... */ }

我稍微改进了缩进。这是 merge() 的最终工作版本:

void merge(int *arr, int start_index,int middle_index, int end_index) {
    int n1,n2,i,l,m;
    n1=middle_index-start_index+1;
    n2=end_index-middle_index;
    int sub_arr1[n1+1],sub_arr2[n2+1];
    for (i=0;i<=(n1-1);i++)
        sub_arr1[i]=arr[start_index+i];
     for (i=0;i<=(n2-1);i++)
        sub_arr2[i]=arr[middle_index+1+i];
    sub_arr1[n1]=100;
    sub_arr2[n2]=100;
    l=0,m=0;
    for (i=start_index;i<=end_index;i++) {
        if(sub_arr1[l]<sub_arr2[m]) {
            arr[i]=sub_arr1[l];
            l=l+1;
        }
        else {
            arr[i]=sub_arr2[m];
            m=m+1;
        }
    }
}

测试和工作。

于 2013-10-05T17:05:17.580 回答