Pandiyan Mani
Jul 22, 2021

--

Merge two array

By using the merge sort algorthim we can achieve it the merging part of merge sort algorthim is used here

Example for Merging two array

import java.util.Arrays;

class Employee
{
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr2[] = { 12, 11};
int arr1size = arr.length;
int arr2size = arr2.length;
int total = arr1size+arr2size;
int[] temp =new int[total];
int i = 0, j = 0;
int k = 0;
while (i < arr1size && j < arr2size)
{
if (arr[i] <= arr2[j])
{
temp[k] = arr[i];
i++;
}
else
{
temp[k] = arr2[j];
j++;
}
k++;
}
while (i < arr1size)
{
temp[k] = arr[i];
i++;
k++;
}
while (j < arr2size)
{
temp[k] = arr2[j];
j++;
k++;
}
System.out.println(Arrays.toString(temp));
}
}

--

--