Merging Two Sorted Arrays in Java

Merging two sorted array 

Here, in this page we will discuss the Java program for merging two  sorted array. For this we will create an another array and insert the elements in efficient manner from the given two arrays.
Merge two sorted array in Java

Merging Two Sorted Arrays In Java

Merging two sorted arrays in Java involves combining two sorted arrays into one sorted array. This can be done efficiently by comparing the first element of each array and adding the smaller one to a new array. The process is repeated until all elements from both arrays are added to the new array. Here is an example:
First Array = [12, 18, 40, 60]
Second Array = [47, 56, 89, 90]

Merged Array = [12, 18, 40, 47, 56, 60, 89, 90]

Merging two-sorted arrays in Java

Algorithm :

  • Take the size of array 1 from the user and store it in variable n1.
  • Input n1 elements from the user and store it in array arr1.
  • Take the size of array 2 from the user and store it in variable n2.
  • Input n2 elements from the user and store it in array arr2.
  • Declare an array of size n1+n2 and named it as arr3.
  • Set one pointer at arr1 and another at arr2.
  • Now, simultaneously traverse arr1 and arr2. Which element is smaller, copy that element in arr3 and move ahead.
  • After the above step and traverse the remaining elements of arr1 or arr2 and copy them in arr3.

Code For Merging Two Sorted Array

Run
import java.util.Scanner;

public class Main {

    public static void merge(int arr1[], int arr2[], int n1, int n2, int arr3[]) {
        int i = 0, j = 0, k = 0;

        while (i < n1 && j < n2) {
            if (arr1[i] < arr2[j])
                arr3[k++] = arr1[i++];
            else
                arr3[k++] = arr2[j++];
        }

        while (i < n1)
            arr3[k++] = arr1[i++];

        while (j < n2)
            arr3[k++] = arr2[j++];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
		int n1 = 4;
		int arr1[] = {12, 18, 40, 60};
		int n2 = 4;
        int arr2[] = {47, 56, 89, 90};
		
		System.out.print("First Array : ");
        for (int i = 0; i < n1; i++)
            System.out.print(arr1[i] + " ");
		
		System.out.print("\nSecond Array : ");
        for (int i = 0; i < n2; i++)
            System.out.print(arr2[i] + " ");
		
        int arr3[] = new int[n1 + n2];
        merge(arr1, arr2, n1, n2, arr3);

        System.out.println("\nArray after merging: ");
        for (int i = 0; i < n1 + n2; i++)
            System.out.print(arr3[i] + " ");

        scanner.close();
    }
}

Output:

First Array : 12 18 40 60 
Second Array : 47 56 89 90 
Array after merging: 
12 18 40 47 56 60 89 90

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java