w3resource

Java: Merge two given sorted array of integers and create a new sorted array


Merge Two Sorted Arrays

Write a Java program to merge two given sorted arrays of integers and create another sorted array.

Example
array1 = [1,2,3,4]
array2 = [2,5,7, 8]
result = [1,2,2,3,4,5,7,8]

Pictorial Presentation:

Java Basic Exercises: Merge two given sorted array of integers and create a new sorted array


Sample Solution:

Java Code:

import java.util.*;

public class Example113 {
    public static void main(String[] arg) {
        // Declare two sorted integer arrays, array1 and array2
        // array1 has 'm' elements but is large enough to accommodate 'm+n' elements
        // array2 has 'n' elements
        
        // Declaration and instantiation of array1
        int array1[] = new int[8];
        
        // Initialize the first four elements of array1
        array1[0] = 1;
        array1[1] = 2;
        array1[2] = 3;
        array1[3] = 4;
        
        // Initialize array2
        int[] array2 = {2, 5, 7, 8};
        
        System.out.println("\nArray1: " + Arrays.toString(array1));
        System.out.println("\nArray2: " + Arrays.toString(array2));
        
        // Define variables m and n
        int m = 4, n = 4;
        
        // Initialize pointers for array1 (i) and array2 (j) and the index for merging (index)
        int i = m - 1, j = n - 1, index = m + n - 1;
        
        // Merge the two arrays into array1
        while (i >= 0 && j >= 0) {
            if (array1[i] > array2[j]) {
                array1[index--] = array1[i--];
            } else {
                array1[index--] = array2[j--];
            }
        }
        
        // Handle remaining elements in array1 (if any)
        while (i >= 0) {
            array1[index--] = array1[i--];
        }
        
        // Handle remaining elements in array2 (if any)
        while (j >= 0) {
            array1[index--] = array2[j--];
        }

        System.out.println("\nMerged array: " + Arrays.toString(array1));
    }
}

Sample Output:

Array1: [1, 2, 3, 4, 0, 0, 0, 0]                                       
                                                                       
Array2: [2, 5, 7, 8]                                                   
                                                                       
Merged array: [1, 2, 2, 3, 4, 5, 7, 8]  

Flowchart:

Flowchart: Java exercises: Merge two given sorted array of integers and create a new sorted array


For more Practice: Solve these Related Problems:

  • Modify the program to merge three sorted arrays instead of two.
  • Write a program to merge arrays while removing duplicates.
  • Modify the program to use recursion to merge the arrays.
  • Write a program to merge two sorted arrays in descending order.

Go to:


PREV : Trailing Zeros in Factorial.
NEXT : Rotate String by Offset.


Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.