Merge two sorted arrays in java


In this core java programming tutorial we will write a program to Merge two sorted arrays in java.



Hi! In this post we will learn how to merge two sorted arrays.

Sorted Array1 :  1 3 7 11
Sorted Array2 :          2 5 8 22
merged array: 1 2 3 5 7 8 11 22

Let’s understand how to Merge two sorted arrays in java by diagram.

LOGIC to Merge two sorted arrays in java>

Initially ar1Index & ar2Index =0,
compare elements in both array at ar1Index and ar2Index using ar1[ar1Index] < ar2[ar2Index]
if element in ar1 is smaller, put that in mergedArray and increment index of both ar1 & mergedArray.
if element in ar2 is smaller, put that in mergedArray and increment index of both ar2 & mergedArray.
continue the process until we reach end index of any of the array and


                        HashMap Custom implementation in java


Example/ Full Program/SourceCode to Merge two sorted arrays in java >
/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class MergeTwoSortedArraysExample {
   public static void main(String[] args) {    
          int[] ar1 = { 1, 3, 7, 11 };
          int[] ar2 = { 2, 5, 8, 22 };
          System.out.print("Display ar1 : "  );
          for (int i = 0; i < ar1.length; i++)
                 System.out.print(ar1[i] +" ");
          System.out.print("\nDisplay ar2 : "  );
          for (int i = 0; i < ar2.length; i++)
                 System.out.print(ar2[i] +" ");
         
          int mergedArray[]=merging(ar1, ar2); //merging both arrays.
          System.out.print("\nDisplay merged array: "  );
          for (int i = 0; i < mergedArray.length; i++)
                 System.out.print(mergedArray[i] +" ");
                
   }
  
   /*
   * Method merges two sorted arrays in java.
   */
   static int[] merging(int[] ar1, int ar2[]) {
         
          int mergedArray[]=new int[ar1.length+ar2.length];
          int ar1Index=0, ar2Index=0, mergedArrayIndex=0;
         
          while (ar1Index < ar1.length  && ar2Index < ar2.length)
                 if (ar1[ar1Index] < ar2[ar2Index])
                       mergedArray[mergedArrayIndex++] = ar1[ar1Index++];
                 else
                       mergedArray[mergedArrayIndex++] = ar2[ar2Index++];
         
          while (ar1Index < ar1.length )
                 mergedArray[mergedArrayIndex++] = ar1[ar1Index++];
         
          while (ar2Index < ar2.length)
                 mergedArray[mergedArrayIndex++] = ar2[ar2Index++];
         
          return mergedArray;
         
   }
}
/*OUTPUT
Display ar1 : 1 3 7 11
Display ar2 : 2 5 8 22
Display merged array: 1 2 3 5 7 8 11 22
*/



So in this core java programming tutorial we wrote a program how to Merge two sorted arrays in java.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>


>Pattern/Pyramid generating programs









No comments:

Post a Comment