See Also:
Example Program To Merge Two Arrays (in C):
C Program To Merge Two Arrays
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
C Program For Array Representation Of Sparse Matrix
C Program To Perform Insertion, Deletion, Searching & Traversal In Singly Linked List
C Program To Perform Insertion, Deletion, Sorting In Doubly Linked List - (Simple)
C Program To Sort A Doubly Linked List (Descending Order)
C Program To Reverse Linked List
C Program To Implement Circular Singly Linked List
C Program To Implement Circular Doubly Linked List
C Program For Polynomial Multiplication Using Linked List
C Program For Polynomial Addition Using Linked List
C Program For Linked List Representation Of Sparse Matrix
C Program To Concatenate Two Linked Lists
C Program To Perform Recursion On Linked List
Insertion, Deletion, Traversal, Reversal And Search Operation on Arrays
Doubly Linked List - Insertion, Traversal, Searching, Delete Node, Delete List
Doubly Linked List - Insertion, Traversal, Searching, Delete Node, Delete List
#include <stdlib.h>
/* sort array in ascending order */
void sortArray(int *array, int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (array[i] > array[j]) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
/* merge i/p arrays "input1" and "input2" to form "output" array */
int * mergeArrays(int *input1, int *input2, int n1, int n2) {
int i, j, k, n, *output;
n = n1 + n2;
i = j = k = 0;
output = (int *)malloc(sizeof (int) * n);
while (i <= n) {
if (input1[k] < input2[j]) {
output[i] = input1[k];
k++;
if (k >= n1) {
while (j < n2) {
i++;
output[i] = input2[j];
j++;
}
}
} else {
output[i] = input2[j];
j++;
if (j >= n2) {
while (k < n1) {
i++;
output[i] = input1[k];
k++;
}
}
}
i++;
}
return output;
}
/* display contents of the given array */
void display(int *array, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int i, n1, n2, *input1, *input2, *output;
printf("Enter the no of elements for array 1 & 2:");
scanf("%d%d", &n1, &n2);
input1 = (int *)malloc(sizeof (int) * n1);
input2 = (int *)malloc(sizeof (int) * n2);
printf("Input for Array 1:\n");
for (i = 0; i < n1; i++) {
printf("input1[%d]:", i);
scanf("%d", &input1[i]);
}
printf("Input for Array 2:\n");
for (i = 0; i < n2; i++) {
printf("input2[%d]:", i);
scanf("%d", &input2[i]);
}
sortArray(input1, n1);
sortArray(input2, n2);
printf("\nInput Array 1:");
display(input1, n1);
printf("Input Array 2:");
display(input2, n2);
output = mergeArrays(input1, input2, n1, n2);
printf("Output Array After Merge Operation:\n");
display(output, n1 + n2);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the no of elements for array 1 & 2:3 4
Input for Array 1:
input1[0]:5
input1[1]:2
input1[2]:4
Input for Array 2:
input2[0]:8
input2[1]:7
input2[2]:10
input2[3]:6
Input Array 1:2 4 5
Input Array 2:6 7 8 10
Output Array After Merge Operation:
2 4 5 6 7 8 10
Enter the no of elements for array 1 & 2:3 4
Input for Array 1:
input1[0]:5
input1[1]:2
input1[2]:4
Input for Array 2:
input2[0]:8
input2[1]:7
input2[2]:10
input2[3]:6
Input Array 1:2 4 5
Input Array 2:6 7 8 10
Output Array After Merge Operation:
2 4 5 6 7 8 10
No comments:
Post a Comment