Question

I need help with this .java implementation: Implement an algorithm that given two sorted arrays of...

I need help with this .java implementation:

Implement an algorithm that given two sorted arrays of size m and n creates a sorted array of size (n + m) in O(n + m) time.

The script must take as an input a command line argument specifying the name of a .txt file which will contain the arrays to be sorted.

Each line in the .txt file will contain two arrays, with elements in arrays separated by commas and the arrays themselves separated by semicolons (e.g. 1,2,7,8;3,4,9,10). For each line in the input file your script should print out the elements in the new sorted array separated by spaces.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code is as follows:

Main.java

************************************************************************************************************************************

import java.io.*;
import java.util.Scanner;
public class Main
{
public static int[] mergeArrays(int arr1[], int arr2[], int m, int n) {
int i = 0, j = 0, k = 0;
int arr3[] = new int[m+n];
// Traverse both array
while (i<m && j <n)
{
// Check if current element of first
// array is smaller than current element
// of second array. If yes, store first
// array element and increment first array
// index. Otherwise do same with second array
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
  
// Store remaining elements of first array
while (i < m)
arr3[k++] = arr1[i++];
  
// Store remaining elements of second array
while (j < n)
arr3[k++] = arr2[j++];
  
return arr3;
}
   public static void main(String[] args) {
       try {
//the file to be opened for reading
FileInputStream fis=new FileInputStream(args[0]);   
Scanner sc=new Scanner(fis); //file to be scanned
//returns true if there is another line to read
while(sc.hasNextLine()){
String s = sc.nextLine(); //read the line in String
String s1[] = s.split(";"); //split the string using ; to get two arrays
String arr1[] = s1[0].split(","); //split the first array with ,
String arr2[] = s1[1].split(","); //split the second array with ,
int array1[] = new int[arr1.length]; //a Integer array to store array 1
int array2[] = new int[arr2.length]; // a Integer array to store array 2
for(int i=0;i<arr1.length;i++){
array1[i] = Integer.parseInt(arr1[i]); //parse the string array and store in int array
}
for(int i=0;i<arr2.length;i++){
array2[i] = Integer.parseInt(arr2[i]); //parse the string array and store in int array

}
int result[] = mergeArrays(array1,array2,array1.length,array2.length); // get the resultant array
for(int i = 0 ; i < result.length ; i++ ){
System.out.print(result[i]+" "); // print the result
}
System.out.println(); // next Line
}
sc.close(); //closes the scanner
}
catch(IOException e){
e.printStackTrace();
}
   }
}

************************************************************************************************************************************

Input file:

arrays.txt

1,2,7,8;3,4,9,10
1,2,5;0,10,15
1,4,10;2,5,19,23
2,4,6,9;1,5,8,14,29

***********************************************************************************************************************************

Screenshot of the code:

text file screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
I need help with this .java implementation: Implement an algorithm that given two sorted arrays of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in...

    in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in descending order. For example [5, 4, 2]. Return an array with all the elements of these two arrays sorted, also in descending order. partb: if the two arrays as input are size n each. What is the big-O run time and space complexity of your implementation public int[] merge(int[] arr1, int[] arr2){

  • You are given an input of k arrays each of size n. Each one of the...

    You are given an input of k arrays each of size n. Each one of the k arrays is sorted. Give an algorithm that turns the k sorted arrays into one sorted array of k * n elements. Please explain the algorithm in words and analyze the run time.

  • -----JAVA---- Implement a linear-time static method called mergeSortedthat merges two sorted Stringarrays, producing a new sorted...

    -----JAVA---- Implement a linear-time static method called mergeSortedthat merges two sorted Stringarrays, producing a new sorted Stringarray. Assume that the input arrays are in ascending (non-decreasing) alphabetic order. For example, if the first input array is [ "cats", "hogs" ] and the second input array is ["dogs", "frogs" ], then mergeSortedreturns the array [ “cats”, “dogs”, “frogs”, “hogs” ]. Note that the inputs may be null or empty. If any input array is null or empty, then the output array...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • I need help with some java code concerning the comparison of two arrays. Let's say I...

    I need help with some java code concerning the comparison of two arrays. Let's say I have two arrays. Array A and Array B. Array A has 7 integers on each line, and there are hundreds of lines. Array B is the same as Array A except every line of integers have been sorted in ascending order. In Array A there are already lines that are sorted, so some lines in Array A will be equal in Array B. What...

  • Need help with my Java Hw: Consider an algorithm that sorts an array of n elements...

    Need help with my Java Hw: Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements and then exchanges those elements with the elements in the first and last positions in the array. Then the size of the array is reduced by two elements after excluding the two elements that are already in the proper positions, and the process is repeated on the remaining part of the array until the entire array is...

  • Suppose that you have two sorted numerical arrays A[1 . . . m] and B[1 ....

    Suppose that you have two sorted numerical arrays A[1 . . . m] and B[1 . . . n]. You want to compute the k’th smallest number in the merged array of all m + n elements. Please design a divide- and-conquer algorithm that can do so in O(log(m + n)). You can assume for simplicity that k is even.

  • 2. Write a Java program to implement Bucket Sort and write a driver to test it....

    2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...

  • Order Statistics: A. Given two sorted arrays X and Y of equal size n, use Quick-Select...

    Order Statistics: A. Given two sorted arrays X and Y of equal size n, use Quick-Select to find the median of all 2n elements in arrays X and Y. Can you improve Quick-Select by choosing better pivots at every step?(in java) B. Show some experiments of your improved Quick-Select with arrays of size n=50. You can assume, if you want, that all the elements in the two arrays are distinct.

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT