Question

Java Programming

Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge sort) Revise Listing 3

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


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

Add a comment
Know the answer?
Add Answer to:
Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge...
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
  • 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

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

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

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

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

  • Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to...

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

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

  • I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on)...

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

  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

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

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