Question

-----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 should contain the elements from the non-empty one.

Hint: Keep an index into each array to process each element. Repeatedly append the smallest unprocessed element from either array and advance the index accordingly.

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

Here is the completed code for this problem in Java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// required method

public static String[] mergeSorted(String arr1[], String arr2[]) {

      // if arr1 is null, initializing arr1 with 0 as length

      if (arr1 == null) {

            arr1 = new String[0];

      }

      // if arr2 is null, initializing arr2 with 0 as length

      if (arr2 == null) {

            arr2 = new String[0];

      }

      // creating a result array with enough capacity

      String result[] = new String[arr1.length + arr2.length];

      // defining three index variables

      int index1 = 0, index2 = 0, index3 = 0;

      // looping as long as index1 is valid in arr1 and index2 is valid in

      // arr2

      while (index1 < arr1.length && index2 < arr2.length) {

            // checking if arr1[index1] is smaller than or equal to arr2[index2]

            if (arr1[index1].compareTo(arr2[index2]) <= 0) {

                  // adding arr1[index1] to result array and incrementing index1

                  result[index3] = arr1[index1];

                  index1++;

            } else {

                  // adding arr2[index2] to result array and incrementing index2

                  result[index3] = arr2[index2];

                  index2++;

            }

            // updating index3

            index3++;

      }

      // now if the lengths of arrays are different, there might be still

      // elements left in either arrays (but not both), if yes, adding all

      // those elements to result

      // adding remaining elements in arr1 (if exists) to result

      while (index1 < arr1.length) {

            result[index3] = arr1[index1];

            index1++;

            index3++;

      }

      // adding remaining elements in arr2 (if exists) to result

      while (index2 < arr2.length) {

            result[index3] = arr2[index2];

            index2++;

            index3++;

      }

      //returning the result array

      return result;

}

Add a comment
Know the answer?
Add Answer to:
-----JAVA---- Implement a linear-time static method called mergeSortedthat merges two sorted Stringarrays, producing a new sorted...
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
  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

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