Question

In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of...

In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of the String class.

The strings are provided as arguments to your main method. In case you haven't yet learned to configure command-line arguments to main in your IDE, now is a good time to learn.

Your program should sort the array of strings using mergesort, then print the strings one per line, in order from smallest ("a") to largest ("z"). The name of your class should be MergeSort.

Example

% java MergeSort z x y w
w
x
y
z

Choices

Your mergesort must have a recursive implementation. That is, you must have a method merge that, in its recursive case, calls itself twice.

On the other hand, you can choose the details of how to implement mergesort. In particular, you may use different indices to indicate subarrays, and have an input and output array. Or, you may copy portions of the array to be sorted, then merge the results. In other words, you can use static storage -- one input array and one output (or spare) array -- or you can use dynamic storage -- subarrays are allocated as needed.

Your merge method may return void, or may return a sorted array -- again, this is your choice.

One thing you are NOT allowed to do is to call a built-in sort method.

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

main.java

import java.util.*;

public class Main {
public static void main(String[] args) {
String[] arr = { "Akshay", "Anthony", "Angelina",
"George", "Victor", "Ankit", "Chama" };
System.out.println("Array after Sorting: ");
String[] arr1 = sorting(arr);
for (int u = 0; u < arr1.length; u++) {
System.out.println(arr1[u] + " ");
}

}

public static String[] sorting(String[] arr2) {
String [] arr3 = new String[arr2.length];
if (arr2.length == 1) {
arr3 = arr2;
} else {
int middle = arr2.length/2;
String[] arr4 = null;
String[] arr5 = null;
if ((arr2.length % 2) == 0) {
arr4 = new String[arr2.length/2];
arr5 = new String[arr2.length/2];
} else {
arr4 = new String[arr2.length/2];
arr5 = new String[(arr2.length/2)+1];
}
int u=0;
int v=0;
for ( ; u < middle; u++) {
arr4[u] = arr2[u];
}
for ( ; u < arr2.length; u++) {
arr5[v++] = arr2[u];
}
arr4 = sorting(arr4);
arr5 = sorting(arr5);
arr3 = merging(arr4,arr5);
}

return arr3;
}

private static String[] merging(String[] arr4, String[] arr5) {
String[] arr6 = new String[arr4.length+arr5.length];
int n = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
while (n < arr4.length || n1 < arr5.length) {
if (n == arr4.length) {
arr6[n2++] = arr5[n1++];
} else if (n1 == arr5.length) {
arr6[n2++] = arr4[n++];
} else {  
n3 = arr4[n].compareTo(arr5[n1]);
if (n3 > 0) {
arr6[n2++] = arr5[n1++];
} else if (n3 < 0) {
arr6[n2++] = arr4[n++];
} else {
arr6[n2++] = arr4[n++];
}
}
}
return arr6;
}


}

Output:

java version 1.8.0 31 ava(TM) SE Runtime Environment (build 1.8.031-b13) Java HotSpot (TM) 64-Bit Server VM (build 25.31-be

Rate an upvote......Thankyou

Hope this helps....

Add a comment
Know the answer?
Add Answer to:
In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method 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
  • 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...

  • use python Write a function that will sort a given list using merge sort. You must...

    use python Write a function that will sort a given list using merge sort. You must use the merge sort algorithm (but may be recursive or iterative). The function will take a list as an input and return a sorted version of the list (you may assume it will be a list of integers). The method signature must be merge_sort(lst).

  • 1.) Complete the merge method below, which is the merge step of MergeSort. That is, it...

    1.) Complete the merge method below, which is the merge step of MergeSort. That is, it takes two sorted arrays as input and returns a new sorted array that contains all the elements from the two input arrays. Furthermore, it should keep all duplicated values, and it should run in O(n) time, where n is the size of the output array. public static int[] main(int[] a, int[] b) { }

  • MERGE SORTING!!! JAVAAAA - Implement the non-recursive version of MergeSort. Also separately, implement the small-size cutoff...

    MERGE SORTING!!! JAVAAAA - Implement the non-recursive version of MergeSort. Also separately, implement the small-size cutoff optimization, with InsertionSort as the small-size sort. (So there should be a regular Merge Sort and an optimized Merge Sort). - All implementations should be in the function. public void mergeSorting (int [] data){ } - Implement the main method with (at least) the following tests: Create tests to make sure the merge works. Test the sort with 10 elements to see if the...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • Problem 5: Recurrence relations and detailed analysis of recursive algorithm efficiency                 g(n: non-negative integer)        &nbsp

    Problem 5: Recurrence relations and detailed analysis of recursive algorithm efficiency                 g(n: non-negative integer)                 1. if n ≤ 1 then return n                 2. else return (5 * g(n─1) ─ 6 * g(n─2)) MergeSort divides the array to be sorted into two equal halves, calls itself recursively on each half to sort that subarray, and then calls the Merge algorithm to merge the two sorted halves in linear time. This leads to its two recurrence relations T(n)=2T(n/2)+cn, n>1;...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a...

    MERGESORT ASSIGNMENT FOLLOW THE FORMAT Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with...

  • Here's my code in C++ so far. I don't understand how to split the array into...

    Here's my code in C++ so far. I don't understand how to split the array into subarrays then sort each subarray to merge into a final sorted array 1. rite a C++ program that implements the merge sort recursive algorithm. For simplicity you may hard-code the list of elements to order. Additionally, you may order elements in increasing or decreasing order, your call. Sample Output Elements to so 8 2 4 69 7 10 1 5 3 Results using merge...

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