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

Instruction using java programming, you will merge two arrays into one array tha

ID: 3764347 • Letter: I

Question

Instruction
using java programming, you will merge two arrays into one array that is in ascending order. You may assume that both

individual arrays are in ascending order. Consider the 2 following examples.

Example 1:

first array is = 2, 26,39, 44, 58, 59, 72

second array is =3, 4, 5, 27, 42, 43, 60, 61, 62

merged array= 2, 3, 4, 5, 26, 27, 39, 42, 43, 44, 58, 59, 60, 61, 62, 72

Example 2:

first array= 12, 14, 16, 18, 20, 22, 24

second array= 2, 4, 6, 8, 10

merged array= 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24

example 3:(used in #5)

first array(unordered)= 8, 3, 13, 5, 10

second array(unordered)= 9, 6, 11, 12, 4, 7, 2

merged array(ordered)= 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

Algorithm

Download the 6 input files (input1_1.txt, input1_2.txt, input2_1.txt, input2_2.txt) from the Blackboard. The file, inputX_1.txt contains the array element for first array and the file, inputX_2.txt contains the array element for second array. You are going to write a program according to the followings:

1. Ask user to enter input file names and read data from the input files and populate the first array and the second array. The file indexed as inputX_1.txt must populate first array and the file indexed as inputX_2.txt must populate the second array. You may need to maintain variables to show how many elements are in each of the arrays. For simplicity, declare all arrays with size 20.

2. Traverse both arrays using a while loop until either one of the arrays has been entirely traversed (i.e., consider the count variables of each of the arrays - countFirst & countSecond - these act as the length of each of the arrays). In the above examples, Example 1 will completely traverse second array and only partially traverse first array. In example 2, first array will be completely traversed and second array will only partially be traversed. You must consider the following:

            a. Use three variables to traverse the indexes in each of the arrays-these will keep track of the current index of the current     element to use/populate in each. indexFirst will keep track of the current index in first, indexSecond the current index in second, and indexMerge the current index in merge.

            b. Within this while-loop, compare the current elements of first and second. If first’s element is smaller, then assign it to merge and increment the appropriate counters. If second's element is smaller, then assign it to merge and increment the appropriate counters.

             c. Note that as long as indexFirst is less than countFirst and indexSecond is less than countSecond you need to keep traversing

3. Once one of the two arrays is completely traversed, you must still finish traversing the array that was only partially traversed. If the remaining array is first, populate the merge array with the remaining elements in first. If the remaining array is second, populate the merge array with the remaining elements in second.

4. Traverse and output the merge array.

5. Modify your program to generate merged array with unordered arrays. You still need to generate ordered merged array as in the Example 3. Use input3_1.txt and input3_2.txt for input data. First, sort the first array and second array and reuse the algorithm described in step 2, 3, and 4. DO NOT use built-in sort function or method.

Explanation / Answer

Here is the logic for you. If you need any further modifications, just get back to me.

import java.io.*;
import java.util.*;
class MergeSort
{
public static void Sort(int Array[], int count)
{
for(int i = 0; i < count-1; i++)
for(int j = 0; j < count-i-1; j++)
if(Array[j] > Array[j+1])
{
int temp = Array[j];
Array[j] = Array[j+1];
Array[j+1] = temp;
}
}
public static void Merge(int Array1[], int Array2[], int count1, int count2)
{
int i = 0, j = 0, k = 0;
int[] Array3 = new int[40];
while(i < count1 && j < count2)
{
if(Array1[i] < Array2[j])
Array3[k++] = Array1[i++];
else
Array3[k++] = Array2[j++];
}
while(i < count1)
Array3[k++] = Array1[i++];
while(j < count2)
Array3[k++] = Array2[j++];
System.out.print("The merged array is: ");
for(i = 0; i < k; i++)
System.out.print(Array3[i]+" ");   
}
public static void main(String[] args) throws IOException
{
Scanner sc1 = new Scanner(System.in);
System.out.print("Enter the name of the first file: ");
String file1 = sc1.next();
System.out.print("Enter the name of the second file: ");
String file2 = sc1.next();
Scanner sc2 = new Scanner(new File(file1));
int[] Array1 = new int[20];
int count1= 0;
while(sc2.hasNextInt())
{
Array1[count1++] = sc2.nextInt();
}
sc2 = new Scanner(new File(file2));
int[] Array2 = new int[20];
int count2 = 0;
while(sc2.hasNextInt())
{
Array2[count2++] = sc2.nextInt();
}
Sort(Array1, count1);
Sort(Array2, count2);
Merge(Array1, Array2, count1, count2);
System.out.println();
}
}