Merging two sorted arrays in C
Merging two sorted array
Here, in this page we will discuss the C program for merging two sorted array. For this we will create an another array and insert the elements in efficient manner from the given two arrays.
Merging Two Sorted Arrays In C
Merging two sorted arrays in C involves combining two sorted arrays into one sorted array. This can be done efficiently by comparing the first element of each array and adding the smaller one to a new array. The process is repeated until all elements from both arrays are added to the new array. Here is an example:
First Array = [12, 18, 40, 60]
Second Array = [47, 56, 89, 90]
Merged Array = [12, 18, 40, 47, 56, 60, 89, 90]
Algorithm :
- Take the size of array 1 from the user and store it in variable n1.
- Input n1 elements from the user and store it in array arr1.
- Take the size of array 2 from the user and store it in variable n2.
- Input n2 elements from the user and store it in array arr2.
- Declare an array of size n1+n2 and named it as arr3.
- Set one pointer at arr1 and another at arr2.
- Now, simultaneously traverse arr1 and arr2. Which element is smaller, copy that element in arr3 and move ahead.
- After the above step and traverse the remaining elements of arr1 or arr2 and copy them in arr3.
Code For Merging Two Sorted Array
#include<stdio.h> void merge(int arr1[], int arr2[], int n1, int n2, int arr3[]) { int i = 0, j = 0, k = 0; while (i< n1 && j < n2) { if (arr1[i] < arr2[j]) arr3[k++] = arr1[i++]; else arr3[k++] = arr2[j++]; } while (i < n1) arr3[k++] = arr1[i++]; while (j < n2) arr3[k++] = arr2[j++]; } int main() { int n1; printf("Enter the number of elements of First Array"); scanf("%d", &n1); printf("Enter the elements of First Array "); int arr1[n1]; for(int i=0; i< n1; i++) scanf("%d", &arr1[i]); printf("Enter the number of elements of Second Array "); int n2; scanf("%d", &n2); printf("Enter of elements of Second Array "); int arr2[n2]; for(int i=0; i< n2; i++) scanf("%d", &arr2[i]); int arr3[n1+n2]; merge(arr1, arr2, n1, n2, arr3); printf("\nArray after merging\n"); for (int i=0; i < n1+n2; i++) printf("%d ", arr3[i]); return 0; }
Output:
Enter the number of elements of First Array 4 Enter the elements of First Array 12 18 40 60 Enter the number of elements of Second Array 4 Enter of elements of Second Array 47 56 89 90 Array after merging 12 18 40 47 56 60 89 90
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
Login/Signup to comment