Description
Merge of two array means copy the elements of first and second array into third array. Compare the corresponding elements of the both array, copy the element into third array from first and second array which is smaller. If the elements of one array become over copy the all elements from another array into third array.
C/C++
/* C program to merge two sorted array*/ //Save it as MergeTwoSortedArray.c #include<stdio.h> int main(){ int i, firstSize, secondSize, firstIndex=0, secondIndex=0, index=0; printf("Enter the size of first array : "); scanf("%d",&firstSize); int firstArray[firstSize]; printf("Enter the elements of first array : "); for(i=0;i<firstSize;i++){ scanf("%d",&firstArray[i]); } printf("Enter the size of second array : "); scanf("%d",&secondSize); int secondArray[secondSize]; printf("Enter the elements of second array : "); for(i=0;i<secondSize;i++){ scanf("%d",&secondArray[i]); } /* Size of merged array is the sum of size of first array and second array*/ int sizeMergeArray = firstSize + secondSize; int mergeArray[sizeMergeArray]; /* Compare the corresponding element of the both the array,copy the element into third array from either first array or second array which is samaller */ while(firstIndex < firstSize && secondIndex < secondSize){ if(firstArray[firstIndex] < secondArray[secondIndex]){ mergeArray[index] = firstArray[firstIndex]; firstIndex++; } else{ mergeArray[index] = secondArray[secondIndex]; secondIndex++; } index++; } /* If elements of first array is copied but still second array has some elements */ if(firstIndex == firstSize){ while(secondIndex < secondSize){ mergeArray[index] = secondArray[secondIndex]; secondIndex++; index++; } } /* If elements of second array is copied but still first array has some elements */ if(secondIndex == secondSize){ while(firstIndex < firstSize){ mergeArray[index] = firstArray[firstIndex]; firstIndex++; index++; } } // Finally print new array after merge of two array printf("The merged array is : "); for(i=0;i<sizeMergeArray;i++){ printf("%d ",mergeArray[i]); } return 0; }
Input: Enter the size of first array : 5 Enter the elements of first array : 2 5 8 11 14 Enter the size of second array : 4 Enter the elements of second array : 7 10 13 16 Output: The merged array is : 2 5 7 8 10 11 13 14 16
Java
/*Java program to merge two sorted array*/ //Save it as MergeTwoSortedArray.java import java.io.*; import java.util.Scanner; public class MergeTwoSortedArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i, firstSize, secondSize, firstIndex=0, secondIndex=0, index=0; System.out.println("Enter the size of first array : "); firstSize = scanner.nextInt(); int firstArray[] = new int[firstSize]; System.out.println("Enter the elements of first array : "); for(i=0;i<firstSize;i++){ firstArray[i] = scanner.nextInt(); } System.out.println("Enter the size of second array : "); secondSize = scanner.nextInt(); int secondArray[] = new int[secondSize]; System.out.println("Enter the elements of second array : "); for(i=0;i<secondSize;i++){ secondArray[i] = scanner.nextInt(); } /* Size of merged array is the sum of size of first array and second array*/ int sizeMergeArray = firstSize + secondSize; int mergeArray[] = new int[sizeMergeArray]; /* Compare the corresponding element of the both the array,copy the element into third array from either first array or second array which is samaller */ while(firstIndex < firstSize && secondIndex < secondSize){ if(firstArray[firstIndex] < secondArray[secondIndex]){ mergeArray[index] = firstArray[firstIndex]; firstIndex++; } else{ mergeArray[index] = secondArray[secondIndex]; secondIndex++; } index++; } /* If elements of first array is copied but still second array has some elements */ if(firstIndex == firstSize){ while(secondIndex < secondSize){ mergeArray[index] = secondArray[secondIndex]; secondIndex++; index++; } } /* If elements of second array is copied but still first array has some elements */ if(secondIndex == secondSize){ while(firstIndex < firstSize){ mergeArray[index] = firstArray[firstIndex]; firstIndex++; index++; } } // Finally print new array after merge of two array System.out.println("The merged array is : "); for(i=0;i<sizeMergeArray;i++){ System.out.print(mergeArray[i]+ " "); } } }
Input: Enter the size of first array : 5 Enter the elements of first array : 2 5 8 11 14 Enter the size of second array : 4 Enter the elements of second array : 7 10 13 16 Output: The merged array is : 2 5 7 8 10 11 13 14 16
Related Programs
1) Find Smallest element and it’s position of an Array2) Program to swap maximum and minimum element of Array
3) Program to read and display 2D array
4) Program to Rearrange an array such that arr[i]=i
5) Program to Find the smallest missing number
6) Program to Find missing odd number in first n odd number
7) Program to Find K largest elements from array
8) Program to Find Value equal to index value
9) Program to Check Array is Perfect or Not
10) Program to make pair of elements alternatively
11) Remove Duplicate Elements From Array