# Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique.

• Compare a and b first. Put whichever is smaller in c, say b.
• Compare and contrast a with b. Put whichever of the two is smaller, say b [1,] into c.
• Compare and contrast a  with b, put whichever is smaller, such as a , into c.
• Compare a  with b and so on. One of the arrays a[] or b[] will eventually be exhausted.
• The remaining elements in the other array are simply copied into c[] at this point.

In file mergesort.h

``````#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
void merge ( int a [ ], int b [ ], int c [ ], int m, int n ) ;
void mergesort( int key [ ], int n )  ;
void wrt( int key [ ], int sz ) ;
in file merge.c
/ * merge a [ ] of size m\n and b [ ] of size n into c [ ] * /
# include “mergesort.h”
void merge ( int a [ ], int b [ ], int c [ ], int m, int n )
{
int i = 0, j = 0, k = 0 ;
while ( i< m && j < n )
if ( a [ i ] < b [ j ] )
c [ k  + + ] = a [ i + + ] ;
else
c [ k  + + ] = b [ j + + ] ;
while ( i< m )
c [ k  + + ] = a [ i + + ] ;
while ( j< n )
c [ k  + + ] = a [ j + + ] ;
}
``````

It is expected that the array c [ ] has adequate area to accommodate both a [ ] and b[ ]. The programmer must ensure that the c[] boundaries are not exceeded. It's worth noting that one or both of the last two while statements, which are intended to gather up any remaining values, are not performed. This is due to the fact that at least one of the two criteria i< m and j< n will be false.

Next, we'll create a mergesort() method that calls merge(). A merge sort is far more efficient than a bubble sort. Our mergesort() function will operate on an array key[, which has a size of a power of two. The "power of two" condition will simplify the explanation. Let's pretend key [] has the following 16 numbers:

The data will be processed by the algorithm in several stages. The table below depicts how we want the data to be appeared after each pass:

We want each succeeding pair of integers to be in order after the first pass. We want each succeeding quartet of integers to be in order after the second pass. We want each subsequent octet of Integers to be in order on the third pass. Finally, we want all 16 integers to be in order after the fourth run. Merge() is used at each stage to get the desired ordering. After the third iteration, for example, we have two sub-arrays.

0    1     3     4     4     8     55     67      and      -5     -1     1     2     4     7     9     37

Both of which are correct. We get the totally sorted array in the last line of the table by combining these two subarrays. Surprisingly, the code that does this is rather quick, demonstrating the power of pointer arithmetic.

``````In file mergesort.c
/ * mergesort : using merge ( ) for sorting an array if size n. * /
#include "mergesort.h"
void mergesort (int keyl], int n)
{
Int J, K, m, * w ;
for ( m = 1 ; m < n ; m * =  2 ) 		/ * m is a power of 2 * /
if ( n< m ) {
print( " ERROR: Array size not a power of 2 – end !  \n " ) ;
exit ( 1 ) ;
}
w= calloc( n, sizeof ( int ) ) ;			/ * allocate workspace * /
assert ( w 1 = NULL ) ;			/ * check that calloc ( ) worked * /
for ( k = 1 ; k < n ; K * = 2 ) {
for ( j = 0 ; j < n – K ; i + =  2 * k )
/ *
/ / Merge two subarrays of key [ ] into a subarray of w [ ].
* /
Merge ( key + j , key + j + k, W + J, k, k ) ;
for ( i = 0; j < n ; + + j )
key [ j ] = w [ j ] ;				/ * write w back into key * /
}
free(w);					/ *  free the workspace * /
}
``````