# merge_two_arrays_of_integers_v1.c

Nov 23rd, 2019
352
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /*
2.     merge_two_arrays_of_integers_v1.c
3.
4. https://www.geeksforgeeks.org/merge-two-sorted-arrays/
5.
6.
7. You can find all my C programs at Dragan Milicev's pastebin:
8.
9. https://pastebin.com/u/dmilicev
10.
12.
13. */
14.
15. #include <stdio.h>
16.
17. // Function to print array, n is number of elements in array arr[]
18. // text[] describes the shown array arr[]
19. void ShowArray(char text[],int arr[],int n)
20. {
21.     int i;
22.
23.     printf("%s",text);
24.     for(i=0;i<n;i++)
25.         printf("%5d", arr[i]);
26.
27.     printf("\n");
28. }
29.
30. // Function to swap two integers, with pointers
31. // https://www.geeksforgeeks.org/c-program-swap-two-numbers/
32. void swap(int *xp, int *yp)
33. {
34.     int temp = *xp;
35.     *xp = *yp;
36.     *yp = temp;
37. }
38.
39. // function bubbleSort to sort the array in ascending order
40. // https://www.geeksforgeeks.org/bubble-sort/
41. void bubbleSort(int arr[], int n)
42. {
43.     int i, j;
44.
45.     for (i = 0; i < n-1; i++)
46.         for (j = 0; j < n-i-1; j++)     // Last i elements are already in place
47.             if (arr[j] > arr[j+1])      // arr[j] < arr[j+1] for descending order
48.                 swap(&arr[j], &arr[j+1]);
49. }
50.
51. void merge_two_sorted_arrays_of_integers( int arr1[], int n1, int arr2[], int n2, int arr3[] )
52. {
53.     int i=0, j=0, k=0;              // indexes for arr1[i], arr2[j], arr3[k]
54.
55.     while (i<n1 && j <n2)           // Traverse both array
56.     {                               // Check if current element of first array
57.         if (arr1[i] < arr2[j])      // is smaller than current element of second array.
58.             arr3[k++] = arr1[i++];  // If yes, store first array element and increment their indexes.
59.         else
60.             arr3[k++] = arr2[j++];  // Otherwise do same with second array.
61.     }
62.
63.     while (i < n1)                  // Store remaining elements of first array
64.         arr3[k++] = arr1[i++];
65.
66.     while (j < n2)                  // Store remaining elements of second array
67.         arr3[k++] = arr2[j++];
68. }
69.
70.
71. // Main program to test above functions
72. int main(void)
73. {
74.     int arr1[] = {2,4,1,3};
75.     int arr2[] = {7,9,8,6,10,5};
76.     int arr3[20];
77.     int n1 = sizeof(arr1)/sizeof(arr1[0]);  // a way to get n1, number of elements in array arr1[]
78.     int n2 = sizeof(arr2)/sizeof(arr2[0]);  // a way to get n2, number of elements in array arr2[]
79.     int n3 = n1+n2;                         // a way to get n3, number of elements in array arr3[]
80.
81.     // If arrays are not sorted, we need to sort them first
82.     ShowArray("\n Unsorted array arr1[] of integers is: \n\n", arr1, n1 );
83.     bubbleSort( arr1, n1 );
84.     ShowArray("\n Array arr1[] sorted by bubble sort is: \n\n", arr1, n1 );
85.
86.     ShowArray("\n Unsorted array arr2[] of integers is: \n\n", arr2, n2 );
87.     bubbleSort( arr2, n2 );
88.     ShowArray("\n Array arr2[] sorted by bubble sort is: \n\n", arr2, n2 );
89.
90.     // now we can merge two sorted arrays arr1[] and arr2[] in arr3[]
91.     merge_two_sorted_arrays_of_integers( arr1, n1, arr2, n2, arr3 );
92.
93.     ShowArray("\n Merged array arr3[] of integers is: \n\n", arr3, n3);
94.
95.
96.     return 0;
97. }