Java Programming. Write your own source code with plenty of comments. (Merge two
ID: 3790699 • Letter: J
Question
Java Programming. Write your own source code with plenty of comments.
(Merge two sorted lists) Write the following method that merges two sorted lists into a new sorted list. public static int[] merge(int[] list1, int[] list2) Implement the method in a way that takes at most list1.length + list2. length comparisons. Write a test program that prompts the user to enter two sorted lists and displays the merged list. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list.
Enter list1: 5 1 5 16 61 111
Enter list2: 4 2 4 5 6
The merged list is 1 2 4 5 5 6 16 61 111
Explanation / Answer
Hi,
Please see below the class. Please comment for any queries/feedbacks.
Thanks,
Anita
ArraySort.java
import java.util.Scanner;
public class ArraySort {
public static void main(String [] args){
//Variable declaration
String sizeOfList1Str = "";
String sizeOfList2Str = "";
int sizeOfList1 = 0;
int sizeOfList2 = 0;
int counter = 0 ;
String currentVal = "";
//list declaration
int [] list1 ,list2,resultArray;
//Scanner object initialization to read inputs fromuser
Scanner scan = new Scanner(System.in);
//Reading list 1 elements
System.out.println("Enter list1: ");
sizeOfList1Str = scan.nextLine();
sizeOfList1 = Integer.valueOf(sizeOfList1Str); //First vaue is the size of list1
//Initializing list 1 with user entered listSize
list1 = new int[sizeOfList1];
while(counter != sizeOfList1){
//Reading and setting the valuse from user input to list1 array
currentVal = scan.nextLine();
list1[counter] = Integer.valueOf(currentVal);
counter ++ ; //incrementing the counter
}
//Reading list 2 elements
counter = 0;
System.out.println("Enter list2: ");
sizeOfList2Str = scan.nextLine();
sizeOfList2 = Integer.valueOf(sizeOfList2Str);//First vaue is the size of list2
//Initializing list 2 with user entered listSize
list2 = new int[sizeOfList2];
while(counter != sizeOfList2){
//Reading and setting the valuse from user input to list2 array
currentVal = scan.nextLine();
list2[counter] = Integer.valueOf(currentVal);
counter ++ ; //incrementing the counter
}
//Calling merge() to sort the input arrays
resultArray = merge(list1,list2);
//Printing the resulted sorted array in loop
System.out.println("The merged list is :");
for(int i =0;i<resultArray.length;i++){
System.out.print(resultArray[i]+" ");
}
}
/**
* @param int[] list1, int [] list2
* This method sorts the two input sorted int array
* return: int[]
*
* */
public static int[] merge(int[] list1, int[] list2) {
//Creating the result array of size - size(list1)+size(list2)
int[] answer = new int[list1.length + list2.length];
int i = 0, j = 0, k = 0;
while (i < list1.length && j < list2.length)
{
//comparing element by element of each array and setting the smaller one to the result array
if (list1[i] < list2[j])
{
answer[k] = list1[i];
i++; //incrementing i to read the next element of list1, j will be the same
}
else
{
answer[k] = list2[j];
j++; //incrementing j to read the next element of list1, i will be the same
}
k++; //incrementing the counter for result array
}
//If list1 > list2, populate remaining elements from list1 to result array
while (i < list1.length)
{
answer[k] = list1[i];
i++;
k++;//incrementing the counter for result array
}
//If list2 > list1, populate remaining elements from list2 to result array
while (j < list2.length)
{
answer[k] = list2[j];
j++;
k++;//incrementing the counter for result array
}
//return the result array
return answer;
}
}
Sample output:
Enter list1:
5
1
5
16
61
111
Enter list2:
4
2
4
5
6
The merged list is :
1 2 4 5 5 6 16 61 111
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.