C Program For Merge Sort

/*Merge Sort*/

#include<stdio.h>
#include<conio.h>
main()
{
int a[10],b[10],c[20],i,j,k,n,n1;
clrscr();
printf("Enter the sizes of the arrays\n");
scanf("%d%d",&n,&n1);
 for(i=0;i<n;i++)
 {
 printf("Enter the values of a[%d]",i);
 scanf("%d",&a[i]);
 }

 for(j=0;j<n1;j++)
 {
 printf("Enter the value of b[%d]",j);
 scanf("%d",&b[j]);
 }

i=0; j=0; k=0;
 while((i<n)&&(j<n1))
 {
  if (a[i]<b[j])
  {
  c[k]=a[i];
  k++;
  i++;
  }
  else
  {
  c[k]=b[j];
  k++;
  j++;
  }
 }

while(i<n)
{
c[k]=a[i];
k++;
i++;
}

while(j<n1)
{
c[k]=b[j];
k++;
j++;
}

printf("The final array after the sorting is \n");
 for(i=0;i<n+n1;i++)
 {
 printf("c[%d]=%d\n",i,c[i]);
 }
}
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment