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

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

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

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

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

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

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

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

  • Sort a queue in java with generic type, below is my approach which has some issues....

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

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

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

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
ADVERTISEMENT