This is the Java Program to Merge Two Arrays in Order.
Given two arrays of integers, merge them into a single sorted array in order.
Example:
Array1: [1,2,3,4,5]
Array2: [2,3,6,7,8]
Output:
Array = [1,2,2,3,3,4,5,6,7,8]
Create a new array of size equal to the sum of the sizes of the two arrays. Now, compare the first elements of the first two arrays and put the smaller element in the new array. Increment the index counter of the array which contains the small element and continues the process. If the elements of any of the arrays are completely copied, then copy the remaining elements of the other array, into the new array.
Here is the source code of the Java Program to Merge Two Arrays in Order. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
// Java Program to Merge Two Arrays in Order.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class InOrderMerge {
// Function to merge the arrays
static int[] mergeArrays(int[] arrayOne,int[] arrayTwo)
{
int totalLength=arrayOne.length +arrayTwo.length;
int[] c=new int[totalLength];
int j,k,index=0;
j=0;
k=0;
while((j!=arrayOne.length) && (k!=arrayTwo.length)){
if(arrayOne[j]<=arrayTwo[k])
{
c[index++]=arrayOne[j++];
}
else
{
c[index++]=arrayTwo[k++];
}
}
while(k!=arrayTwo.length)
{
c[index++]=arrayTwo[k++];
}
while(j!=arrayOne.length)
{
c[index++]=arrayOne[j++];
}
return c;
}
// Function to read input and display the output
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int m, n;
System.out.println("Enter the size of the two arrays");
try {
n = Integer.parseInt(br.readLine());
m = Integer.parseInt(br.readLine());
}
catch (IOException e)
{
System.out.println("Invalid input");
return;
}
int[] arrayOne = new int[n];
int[] arrayTwo = new int[m];
System.out.println("Enter the first array elements");
int i,j;
for(i=0; i<arrayOne.length; i++){
try {
arrayOne[i] = Integer.parseInt(br.readLine());
}
catch (IOException e)
{
System.out.println("Invalid array element. Enter it again");
i--;
}
}
System.out.println("Enter the second array elements");
for(i=0; i<arrayTwo.length; i++){
try {
arrayTwo[i] = Integer.parseInt(br.readLine());
}
catch (IOException e)
{
System.out.println("Invalid array element. Enter it again");
i--;
}
}
Arrays.sort(arrayOne);
Arrays.sort(arrayTwo);
int[] mergedArray=mergeArrays(arrayOne,arrayTwo);
System.out.println("The merged array is");
for(i=0;i<mergedArray.length;i++)
{
System.out.print(mergedArray[i]+" ");
}
}
}
1. In function mergeArrays(), a new array c is created whose size is equal to the total size of the two arrays.
2. The loop while((j!=arrayOne.length) && (k!=arrayTwo.length)) merges the two array in ascending order, until one of the arrays is completely merged.
3. The loops while(k!=arrayTwo.length) and while(j!=arrayOne.length) merges the remaining components of any array into the final array c.
Time Complexity: O(n*log(n) + m*log(m)) where n is the number of elements in the first array and m is the number of elements in the second array.
Case 1 (Simple Test Case): Enter the size of the two arrays 5 5 Enter the first array elements 1 2 3 4 5 Enter the second array elements 2 3 6 7 8 The merged array is 1 2 2 3 3 4 5 6 7 8 Case 2 (Simple Test Case - another example): Enter the size of the two arrays 4 5 Enter the first array elements 89 45 32 6 Enter the second array elements 24 456 23 567 34 The merged array is 6 23 24 32 34 45 89 456 567
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Apply for Java Internship
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Programming Books