C PROGRAM TO MERGE AND SORT TWO UNSORTED ARRAYS WITH OUTPUT




MERGE AND SORT TWO UNSORTED ARRAYS

Problem:

Let us consider two unsorted arrays of different lengths and our goal is to merge and sort these unsorted arrays into a single sorted array.

Program:



#include<stdio.h>
void sort(int[],int);
void main ()
 {
   int a[10],b[10],c[20],m,n,i;
   printf("Enter size of first array:\n");
   scanf("%d",&m);
   printf("Enter Elements");
   for(i=0;i<m;i++)
    {
     scanf("%d",&a[i]);
     c[i]=a[i];
    }
   printf("Enter size of second array:\n");
   scanf("%d",&n);
   printf("Enter Elements");
   for(i=0;i<n;i++)
    {
     scanf("%d",&b[i]);
     c[m+i]=b[i];
    }
   sort(c,m+n);
   for(i=0;i<m+n;i++)
    {
     printf("%4d",c[i]);
    }
 }
void sort(int c[],int n)
 {
   int i,j,temp;
   for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-i-1;j++)
      {
        if(c[j+1]<c[j])
         {
          temp=c[j];
          c[j]=c[j+1];
          c[j+1]=temp;
         }
      }
    }
 }

Output:

Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment