Below is a simple implementation of the merge sort algorithm in the C programming language. Merge sort is a divide-and-conquer algorithm that recursively divides an array into two halves, sorts each half, and then merges the sorted halves.
Program :
#include <stdio.h>
#include <conio.h>
Â
void main( )
{
int a[5];
int b[5];
int c[10] ;
int i,n,j, k, temp ;
printf(“Enter the Elements of the first Array : “);
for(i=0;i<5;i++)
{
Â
scanf(“%d”,&a[i]);
}
printf(“Enter the Elements of the Second Array : “);
for(i=0;i<5;i++)
{
Â
scanf(“%d”,&b[i]);
}
Â
printf ( “Merge sort.\n” ) ;
Â
printf ( “\nFirst array:\n” ) ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( “%d\t”, a[i] ) ;
Â
printf ( “\n\nSecond array:\n” ) ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( “%d\t”, b[i] ) ;
Â
for ( i = 0 ; i <= 3 ; i++ )
{
for ( j = i + 1 ; j <= 4 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
if ( b[i] > b[j] )
{
temp = b[i] ;
b[i] = b[j] ;
b[j] = temp ;
}
}
}
Â
for ( i = j = k = 0 ; i <= 9 ; )
{
if ( a[j] <=Â b[k] )
c[i++] = a[j++] ;
else
c[i++] = b[k++] ;
Â
if ( j == 5 || k == 5 )
break ;
}
Â
for ( ; j <= 4 ; )
c[i++] = a[j++] ;
Â
for ( ; k <= 4 ; )
c[i++] = b[k++] ;
Â
printf ( “\n\nArray after sorting:\n”) ;
for ( i = 0 ; i <= 9 ; i++ )
printf ( “%d\t”, c[i] ) ;
Â
getch( ) ;
}