Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a \"merge\" program that merges two ordered lists of integers into a new o

ID: 3799299 • Letter: W

Question

Write a "merge" program that merges two ordered lists of integers into a new ordered list. For example, given two ordered lists (1, 4, 6, 9) and (0, 2, 3, 7) as input arguments, "merge" should produce a new list (0, 1, 2, 3, 4, 6, 7, 9) which is also ordered. Another example could be to merge (-3, 0, 6) and (-2, 0, 4, 5, 9) to produce (-2, -3, 0, 0, 4, 5, 6, 9). The "merge" program assumes that the two input lists (in increasing order) of integers are stored in the data area. It loads the integers and merges them into an ordered list. The resulting ordered list (e.g. (-2, -3, 0, 0, 4, 5, 6, 9)) should be stored back into the data area. It is at your own choice how the data area (i.e. the lists) is arranged, and whether the resulting list is overwritten onto the original two lists. But be sure to give meaningful labels and clearly indicate (using label or comments) where the merged list is stored. Before your program terminates, it should print out the merged list which should be in increasing order.

Explanation / Answer

Merging.java :

package org.chegg;

import java.util.ArrayList;

import java.util.Scanner;

public class Merging {

    public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter size of list1:");

       int n1 = sc.nextInt();

       ArrayList<Integer> list1 = new ArrayList<Integer>();

       System.out.println("Enter elements of List1 in increasing order:");

       for(int i=0;i<n1;i++)

           list1.add(sc.nextInt());

       System.out.println("Enter size of list2:");

       int n2 = sc.nextInt();

       ArrayList<Integer> list2 = new ArrayList<Integer>();

       System.out.println("Enter elements of List2 in increasing order:");

       for(int i=0;i<n1;i++)

           list2.add(sc.nextInt());

       ArrayList<Integer> merge = Merge_Algorithm(list1,list2);

       System.out.println("Merge List in increasing order:");

       for(int num : merge)

           System.out.print(num+" ");

    }

    public static ArrayList<Integer> Merge_Algorithm(ArrayList<Integer> list1,ArrayList<Integer> list2){

       ArrayList<Integer> al = new ArrayList<Integer>();

       while(!list1.isEmpty() &&!list2.isEmpty())

       {

           if(list1.get(0) <= list2.get(0)){

               al.add(list1.get(0));

               list1.remove(0);

           }

           else{

               al.add(list2.get(0));

               list2.remove(0);

           }

       }

       while(!list1.isEmpty()){

           al.add(list1.get(0));

           list1.remove(0);

       }

       while(!list2.isEmpty()){

           al.add(list2.get(0));

           list2.remove(0);

       }

       return al;

    }

}

Sample Input & Output:

Enter size of list1:

4

Enter elements of List1 in increasing order:

1 4 6 9

Enter size of list2:

4

Enter elements of List2 in increasing order:

0 2 3 7

Merge List in increasing order:

0 1 2 3 4 6 7 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote