Question

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){

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

package sort;

public class Merge {

public int[] merge(int[] arr1, int[] arr2){

int t[]=new int[arr1.length+arr2.length];//make temp array of size with sum of two array

int k=0;

int k1=0; //from those two temp array copy elements in sorted array in original array

int l=0;

while(k<arr1.length && k1<arr2.length){

if(arr1[k]>=arr2[k1]){ //if arr1's element is less first copy it

t[l++]=arr1[k++];

}

else{

t[l++]=arr2[k1++]; //else arr2's

}

}

while(k<arr1.length){ //copy the remaining elements

t[l++]=arr1[k++];

}

while(k1<arr2.length){

t[l++]=arr2[k1++];

}

return t; //return array

}

//driver program to test

public static void main(String[] args) {

Merge m=new Merge();

int arr1[]={6,5,4,3,2,1};

int arr2[]={67,12,9,8,6};

int t[]=m.merge(arr1, arr2);

for(int i:t){

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

}

}

}

output

67 12 9 8 6 6 5 4 3 2 1

Add a comment
Know the answer?
Add Answer to:
in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in...
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
  • (9) Complete the below Java method recursively, which takes in a String and returns if it's...

    (9) Complete the below Java method recursively, which takes in a String and returns if it's a palindrome. Remember a palindrome means a string that is the same reversed, like "deed", "racecar" or "tacocat". The empty string "" is also a palindrome. public boolean isPalindrome (String word) { (8) (a) (8 points) Implement merging two sorted arrays into one sorted ar- ray. These arrays are sorted in descending order. For example, (5, 4, 2). Return an array with all the...

  • 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...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • *c language* Write a program that takes two sorted array A,B of size m,n respectively where...

    *c language* Write a program that takes two sorted array A,B of size m,n respectively where A is sorted in the ascending order and B is sorted in the descending order, and merge these two arrays into a sorted array C.   For example if A=[1, 4, 5, 7, 8] and B=[10,9,8,6,4,2]   then C=[1,2,4,4,5,6,7,8,8,9,10].

  • -----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...

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • 10 L 1 22 Given the function below, that divides the original array in two arrays...

    10 L 1 22 Given the function below, that divides the original array in two arrays of half size, develop the Java code that combines two sorted arrays or clearly explain how to implement the merge function. (3 points) nt middle values.Length/2; // divide array into two arrays of half size nt left- new int[middle]; or Cint i-e; imiddle; i+) t Leftri] - values[iJ: ntD right- new int[values. length-middle]: or Cint i-a; i<values.length-middle; i++) t rightli] -values [middle+i]; ort(left); //recursively...

  • Using Java solve recursively without the use of loops or modifying the method signatures. /** *...

    Using Java solve recursively without the use of loops or modifying the method signatures. /** * Given a sorted input array a, return a sorted array of size a.length + 1, * consisting of all elements of array a and integer i. * * @param a an array that is sorted in a non-descending order * @param i an integer * @return a sorted array of size a.length + 1, consisting of all elements of * array a and integer...

  • Implement a method to build an AVL tree out of a sorted (ascending order) array of...

    Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You may use any of...

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