import java.util.Random;
import java.util.concurrent.*;
public class GenericParallelMergeSort {
public static void main(String[] args) {
int size = 2000;
Integer[] intArray = new
Integer[size];
for(int i=0; i<size; i++)
{
intArray[i] =
(int)(Math.random()*1000);
}
System.out.print("Integer Array
List: " );
for (int i = 0; i < size; i++)
{
System.out.print(intArray[i] + ", ");
}
System.out.println();
parallelMergeSort(intArray);
System.out.print("Sorted Integer
Array List: " );
for (int i = 0; i < size; i++)
{
System.out.print(intArray[i] + ", ");
}
System.out.println();
System.out.println();
//Generating random strings
Random rand = new Random();
String Alphabets =
"abcdefghijklmnopqrstuvwxyz";
String[] strArray = new
String[size];
for(int i = 0; i <
strArray.length; i++) {
String Str1 =
"";
int randStrLen =
5;
for(int j = 0;
j<randStrLen; j++) {
char c =
Alphabets.charAt(rand.nextInt(26));
Str1 = Str1 + c;
strArray[i] = Str1;
}
}
//print string array
System.out.print("Random String
Array List: " );
for (int i = 0; i < size; i++)
{
System.out.print(strArray[i] + ", ");
}
System.out.println();
parallelMergeSort(strArray);
System.out.print("Sorted String
Array List: " );
for (int i = 0; i < size; i++)
{
System.out.print(strArray[i] + ", ");
}
}
public static <E extends Comparable<E>>
void parallelMergeSort(E[] list) {
RecursiveAction mainTask = new
sortTask<E>(list);
ForkJoinPool pool = new
ForkJoinPool();
pool.invoke(mainTask);
}
private static class sortTask <E extends
Comparable<E>> extends RecursiveAction {
private E[] list;
private int Threshold = 500;
public sortTask(E[] list) {
this.list =
list;
}
@Override
protected void compute() {
if(list.length
< Threshold) {
java.util.Arrays.sort(list);
}
else {
//obtain the first part
E[] firstHalf = (E[])(new
Comparable[list.length/2]);
System.arraycopy(list, 0, firstHalf, 0,
list.length/2);
//obtain the second half
int secondHalfLength = list.length -
list.length/2;
E[] secondHalf = (E[])(new
Comparable[secondHalfLength]);
System.arraycopy(list, list.length/2,
secondHalf, 0, secondHalfLength);
//Recursively sort the two halves
invokeAll(new sortTask(firstHalf), new
sortTask(secondHalf));
//Merge first half with second half into list
merge(firstHalf, secondHalf, list);
}
}
public static <E extends
Comparable<E>> void merge(E[] list1, E[] list2, E[] temp)
{
int current1 =
0; // Current index in list1
int current2 =
0; // Current index in list2
int current3 =
0; // Current index in temp
while
(current1 < list1.length && current2 < list2.length)
{
if (list1[current1].compareTo(list2[current2])
< 0 )
temp[current3++] =
list1[current1++];
else
temp[current3++] =
list2[current2++];
}
while
(current1 < list1.length)
temp[current3++] = list1[current1++];
while
(current2 < list2.length)
temp[current3++] = list2[current2++];
}
}
}
![Problems <terminated Generic ParallelMergeSort [Java Application] C:\Program Files\Javaljre18.0 201bin\javaw.exe (Jun 6, 2019](http://img.homeworklib.com/questions/1d31c610-0f49-11ec-b878-9fdec071ee19.png?x-oss-process=image/resize,w_560)
Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel...
Java Programming
Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge sort) Revise Listing 30.10, ParallelMergeSort.java, to define a generic parallelMergeSort method as follows: public static <E extends Comparable<E>> void parallel MergeSort(E list) In the main) method, create an array of integers, print the array of integers, then use the parallelMergeSort() on the array, and print the sorted array. Then, repeat the steps for an array of strings
Exercise 32.13 Follow the instructions in...
1. Implement generic insertion sort public static <E extends Comparable<E>> void insertionSort(E[] list){ //implement body } 2. Implement generic bubble sort public static <E extends Comparable<E>> void bubbleSort(E[] list){ //implement body } 3. Implement generic merge sort public static <E extends Comparable<E>> void mergeSort(E[] list){ //implement body } 4. Implement generic heap sort public static <E extends Comparable<E>> void heapSort(E[] list){ //implement body }
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
Program with generic merge sort and binary search
method help. The programming language I'm using is Java.
This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...
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...
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,...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...
Sort a queue in java with generic type, below is my approach
which has some issues. The porpose is to implement a method to sort
the elements of the queue in ascending order, but I am not sure how
to deal with generic type T instead of int or string. Need your
help to solve this problem. Please post the correct code and the
idea, thanks!
public static <T extends Comparable<T>> int minval(Queue<T> queue, int sort) { int min_index =...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...