Merge Two Sorted Arrays in Java

Merge Two Sorted Arrays in Java | Array Programs in Java – 11 | In the previous Java program, we have developed programs to merge two arrays in Java. Now in this post, we will discuss how to merge two sorted arrays in Java.

Program description:- Write a program to merge two sorted arrays into one sorted array in Java.

Merging of two arrays in Java can be done by using pre-defined methods, or by using Java collections or we can do it manually by using loops. After merging we can sort them, or we can sort them while merging.

Example of merging of two int arrays,
Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Then the result should be,
Merged Array = [9, 10, 18, 20, 27, 30, 36, 40, 45, 50]

Example of merging of two String array,
Array1 = [C++, Java, Python]
Array2 = [CSS, HTML, JavaScript]
Then the result should be,
Merged Array = [C++, CSS, HTML, Java, JavaScript, Python]

Different approaches to solve this problem,
Method1:- First merge both arrays, and then sort the resultant array.
Method2:- While merging both arrays assign elements in sorted order.

Merge Two Sorted Arrays in Java Using Arrays.sort()

In this method, first, merge both arrays to the new array and then sort them using any sorting algorithm. In sort two arrays in Java, we had discussed different approaches to merge the array. After merging them, we will use Arrays.sort() method and most of the time it gives better performance compared to other sorting algorithms. Now, let us demonstrate it through an example.

import java.util.Arrays;

public class CopyArray {

   public static void main(String[] args) {
      
      // array which should be merged
      int src1[] = {10, 20, 30, 40, 50};
      int src2[] = {9, 18, 27, 36, 45};
      
      // create new array 
      int newArray[] = new int[src1.length + src2.length];
      
      // Copy first to new array from 0 to src1.length
      System.arraycopy(src1, 0, newArray, 0, src1.length);
      // copy second array to new array
      System.arraycopy(src2, 0, newArray, src1.length, src2.length);
      
      // sort new array
      Arrays.sort(newArray);
      
      // display all array
      System.out.println("Array1 = " + Arrays.toString(src1));
      System.out.println("Array2 = " + Arrays.toString(src2));
      System.out.println("Merged Array = " 
                      + Arrays.toString(newArray));
   }
}

Output:-

Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Merged Array = [9, 10, 18, 20, 27, 30, 36, 40, 45, 50]

In this program, to display the array we have used the toString() method of java.util.Arrays class that is given to convert array to String.

Method2

In this method,

a) Assume the arrays are src1[], and src2[]
b) Create a new array (dest[]) of the size of src1.length + src2.length
c) Traverse both arrays, pick smaller current elements in src1[] and src2[]. Compare the current element of both arrays, copy the smaller element among them to the next position in dest[] and move ahead in dest[] and the array whose element is picked.
d) If there are remaining elements in src1 or src2 then copy them in dest[]

import java.util.Arrays;

public class CopyArray {
   public static void main(String[] args) {

      // array which should be merged
      int src1[] = { 10, 20, 30, 40, 50 };
      int src2[] = { 9, 18, 27, 36, 45 };

      // resultant array
      int newArray[] = mergeSort(src1, src2);

      // display all array
      System.out.println("Array1 = " + Arrays.toString(src1));
      System.out.println("Array2 = " + Arrays.toString(src2));
      System.out.println("Merged Array = " 
                        + Arrays.toString(newArray));
   }

   private static int[] mergeSort(int[] src1, int[] src2) {
      // create new array
      int merge[] = new int[src1.length + src2.length];

      // variables
      int i = 0, j = 0, k = 0;

      // traverse both array
      while (i < src1.length && j < src2.length) {
         // Compare current element of both array.
         // Store smaller element and
         // increment index of that array
         if (src1[i] < src2[j])
            merge[k++] = src1[i++]; // first array
         else
            merge[k++] = src2[j++]; // second array
      }

      // Store remaining elements of first array
      while (i < src1.length) {
         merge[k++] = src1[i++];
      }

      // Store remaining elements of second array
      while (j < src2.length) {
         merge[k++] = src2[j++];
      }

      return merge;
   }
}

Output:-

Array1 = [10, 20, 30, 40, 50]
Array2 = [9, 18, 27, 36, 45]
Merged Array = [9, 10, 18, 20, 27, 30, 36, 40, 45, 50]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *