Advertisement
Shailrshah

Merging sorted Arrays

Apr 18th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     int l1,l2;
  5.     int a[100],b[100],c[200];
  6.     int i,j,temp;
  7.     printf("Enter the limit of the two arrays.\n");
  8.     scanf("%d%d",&l1,&l2);
  9.     printf("Now enter the first array.\n");
  10.     for(i=0; i<l1; i++)
  11.         scanf("%d",&a[i]);
  12.     for (i = 0 ; i < ( l1 - 1 ); i++)
  13.     {
  14.             for (j = 0 ; j < l1 - i - 1; j++)
  15.             {
  16.                 if (a[j] > a[j+1])
  17.             {
  18.                     temp = a[j];
  19.                     a[j] = a[j+1];
  20.                     a[j+1] = temp;
  21.                 }
  22.         }
  23.     }
  24.     printf("Now enter the second array.\n");
  25.     for(i=0; i<l2; i++)
  26.         scanf("%d",&b[i]);
  27.     for (i = 0 ; i < ( l2 - 1 ); i++)
  28.     {
  29.         for (j = 0 ; j < l2 - i - 1; j++)
  30.             {
  31.                 if (b[j] > b[j+1])
  32.                 {
  33.                     temp = b[j];
  34.                     b[j] = b[j+1];
  35.                     b[j+1] = temp;
  36.                 }
  37.         }
  38.     }
  39.  
  40.     printf("The third array is:-");
  41.     for(i=0; i<l1; i++)
  42.         c[i] = a[i];
  43.     for(i=l1; i<l1+l2; i++)
  44.         c[i] = b[i-l1];
  45.  
  46.     for (i = 0 ; i < (l1 + l2 - 1 ); i++)
  47.     {
  48.         for (j = 0 ; j < l1 + l2 - i - 1; j++)
  49.             {
  50.                 if (c[j] > c[j+1])
  51.                 {
  52.                     temp = c[j];
  53.                     c[j] = c[j+1];
  54.                     c[j+1] = temp;
  55.                 }
  56.         }
  57.     }
  58.     for(i=0; i<l1+l2; i++)
  59.         printf("%d ",c[i]);
  60. }
  61.  
  62. //Output:-
  63. //Enter the limit of the two arrays.
  64. //5
  65. //5
  66. //Now enter the first array.
  67. //1
  68. //2
  69. //3
  70. //4
  71. //5
  72. //Now enter the second array.
  73. //5
  74. //4
  75. //3
  76. //2
  77. //1
  78. //The third array is:-1 1 2 2 3 3 4 4 5 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement