sahajjain01

10.Merge two given sorted arrays.

Aug 15th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.57 KB | None | 0 0
  1. //Program to merge two given sorted arrays.
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. void main () {
  6.     int i = 0, j = 0, k = 0, arr3[20],
  7.     arr1[10] = {
  8.         1, 3, 5, 7, 9, 11, 13, 15, 17, 19
  9.     },
  10.     arr2[10] = {
  11.         2, 4, 6, 8, 10, 12, 14, 16, 18, 20
  12.     };
  13.  
  14.     clrscr ();
  15.  
  16.     while (i < 10 && j < 10) {
  17.         if (arr1[i] < arr2[j]) {
  18.             arr3[k] = arr1[i];
  19.             i++;
  20.         } else {
  21.             arr3[k] = arr2[j];
  22.             j++;
  23.         }
  24.         k++;
  25.     }
  26.  
  27.     if (arr1[9] > arr2[9])
  28.         arr3[19] = arr1[9];
  29.     else
  30.         arr3[19] = arr2[9];
  31.  
  32.     for (i = 0; i < 20; i++) {
  33.         printf ("%d\n", arr3[i]);
  34.     }
  35.  
  36.     getch ();
  37. }
Add Comment
Please, Sign In to add comment