MergeSort Implementation

Hello,

I implemented merge sort using C for an array of size 5000. The second dimension of the ray is just extra data that gets moved along with with first row. The code below works for my testcases. Does this code look like a correct implementation of the mergesort (sometimes the bounds are tricky). This code will sort [begin,end] (ie its includes the last element). Thanks so much,

Eric

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void merge(int begin,int end, int array[5000][2]){
        if (begin==end)
                return;

        merge(begin,(begin+end)/2,array);
        merge((begin+end)/2+1,end,array);

        int a[end-begin+1][2];

        int i,j=begin,k=(begin+end)/2+1;
        for (i=0;i<=end-begin;i++)
                if (j==(begin+end)/2+1){
                        a[i][0]=array[k][0];
                        a[i][1]=array[k++][1];
                }
                else if (k>end){
                        a[i][0]=array[j][0];
                        a[i][1]=array[j++][1];
                }
                else if (array[j][0]>array[k][0]){
                        a[i][0]=array[k][0];
                        a[i][1]=array[k++][1];
                }
                else{
                        a[i][0]=array[j][0];
                        a[i][1]=array[j++][1];
                }
        for (i=begin; i <= end;i++){
                array[i][0]=a[i-begin][0];
                array[i][1]=a[i-begin][1];
        }
}
Last edited on
Topic archived. No new replies allowed.