Advertisement
Guest User

Merge

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. int main()
  4.     {
  5.         int a[10],b[10],c[20];
  6.         int n1,n2;
  7.         int i,j,k;
  8.  
  9.         printf("Enter size of array 1: \n");
  10.         scanf("%d",&n1);
  11.  
  12.         printf("Enter elements of array 1: \n");
  13.         for(i=0;i<n1;i++)
  14.             scanf("%d",&a[i]);
  15.  
  16.  
  17.         printf("Enter size of array 2:\n ");
  18.             scanf("%d",&n2);
  19.  
  20.  
  21.         printf("Enter elements of array 2: \n");
  22.         for(j=0;j<n2;j++)
  23.             scanf("%d",&b[j]);
  24.  
  25.         i=0;
  26.         j=0;
  27.         k=0;
  28.  
  29.         while(i<n1 && j<n2)
  30.         {
  31.             if(a[i]<b[j])
  32.             {
  33.                 c[k]= a[i];
  34.                 i++;
  35.                 k++;
  36.             }
  37.            
  38.             else
  39.             {
  40.                 c[k]=b[j];
  41.                 j++;
  42.                 k++;
  43.             }
  44.  
  45.         }
  46.  
  47.  
  48.         while(i<n1)
  49.         {
  50.             c[k] = a[i];
  51.             i++;
  52.             k++;
  53.         }
  54.  
  55.         while(j<n2)
  56.         {
  57.             c[k] = b[j];
  58.             j++;
  59.             k++;
  60.         }
  61.  
  62.  
  63.  
  64.  
  65.         printf("\nMerged array after operations is :\n");
  66.         for(i=0;i<(n1+n2);i++)
  67.         {
  68.             printf("%d\n",c[i]);
  69.         }
  70.  
  71.         return 0;
  72.  
  73.  
  74.     }
  75.  
  76.  
  77. /* Comment
  78.  
  79. Normal lauda O(nlogn) <- Time complexity
  80.  
  81. Maile lagakop algo lagauda chai O(n)
  82.  Time complexity
  83.  
  84. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement