Question

IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...

IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests.

Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]

import java.util.Scanner;
import java.util.ArrayList;

public class Solution {
public void merge(int [] nums1, int m, int [] nums2, int n){
//Add code below
}
public static void main(String [] args) {
//Variables
Scanner scnr = new Scanner(System.in);
ArrayList<Integer> arrList = new ArrayList<Integer>();
int [] arr1, arr2;
int split = 0;
Solution test = new Solution();
//Remaining values will be added to the array
while(scnr.hasNext()){
arrList.add(scnr.nextInt());
//Get position to mark end of arr1 and beginning of arr2
if(arrList.size() >= 2 && arrList.get(arrList.size()-1) <= arrList.get(arrList.size()-2))
split = arrList.size()-1;
}
//Instance primitive array to size of ArrayList
arr1 = new int[arrList.size()];
arr2 = new int[arrList.size()-(split)];
//Copy elements from ArrayList to primitive array
for(int i = 0; i < (split); i++)
arr1[i] = arrList.get(i);
for(int i = 0; i < arrList.size()-(split); i++)
arr2[i] = arrList.get(split+i);
//Test values
test.merge(arr1,split,arr2,arrList.size()-(split));
//Prints elements in arr1 after merge
for(int i : arr1)
System.out.println(i);
}
}

Homework Answers

Answer #1
import java.util.Scanner;
import java.util.ArrayList;

public class Solution {
    public void merge(int nums1[], int m, int nums2[], int n){
        int i = 0,j = 0,k = 0;
        int arr[] = new int[m+n];
        while(i<m && j<n){
            if(nums1[i] <= nums2[j]){
                arr[k++] = nums1[i++];
            }
            else {
                arr[k++] = nums2[j++];
            }
        }
        while (i<m){
            arr[k++] = nums1[i++];
        }
        while (j<n){
            arr[k++] = nums2[j++];
        }
        for(int t = 0;t<arr.length;t++){
            nums1[t] = arr[t];
        }
    }

    public static void main(String [] args) {
//Variables
        Scanner scnr = new Scanner(System.in);
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        int [] arr1, arr2;
        int split = 0;
        Solution test = new Solution();
//Remaining values will be added to the array
        while(scnr.hasNext()){
            arrList.add(scnr.nextInt());
//Get position to mark end of arr1 and beginning of arr2
            if(arrList.size() >= 2 && arrList.get(arrList.size()-1) <= arrList.get(arrList.size()-2))
                split = arrList.size()-1;
        }
//Instance primitive array to size of ArrayList
        arr1 = new int[arrList.size()];
        arr2 = new int[arrList.size()-(split)];
//Copy elements from ArrayList to primitive array
        for(int i = 0; i < (split); i++)
            arr1[i] = arrList.get(i);
        for(int i = 0; i < arrList.size()-(split); i++)
            arr2[i] = arrList.get(split+i);
//Test values
        test.merge(arr1,split,arr2,arrList.size()-(split));
//Prints elements in arr1 after merge
        for(int i : arr1)
            System.out.println(i);
    }
}

Know the answer?
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
Similar Questions
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort...
IN JAVA PLEASE This problem requires you to code the Merge part of the Merge Sort algorithm. The Merge step merges n elements which takes O(n) time. Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Any code that is found to exceed linear time will fail the tests. Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total...
Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. Java COde: import java.util.Scanner; public class SumOfExcess { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS = 4; int[] testGrades...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
Write a for loop to print all elements in courseGrades, following each element with a space...
Write a for loop to print all elements in courseGrades, following each element with a space (including the last). Print forwards, then backwards. End each loop with a newline. Ex: If courseGrades = {7, 9, 11, 10}, print: 7 9 11 10 10 11 9 7 Hint: Use two for loops. Second loop starts with i = courseGrades.length - 1. (Notes) IN JAVA: import java.util.Scanner; public class CourseGradePrinter { public static void main (String [] args) { Scanner scnr =...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with...
4.9.1: Nested loops: Indent text. JAVA Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Please use my template import java.util.Scanner; public class...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT