Question

Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java...

Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java and a minimum of two threads utilised.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Java program to implement parallel merge sort.
package net.coderodde.util.sorting;

import java.util.Arrays;

/**
 *  implementing a parallel merge sort.
 * 
 
 */
public class ParallelMergesort {

    private static final int MINIMUM_THREAD_WORKLOAD = 100_000;

    public static <T extends Comparable<? super T>> void sort(T[] array) {
        sort(array, 0, array.length);
    }

    public static <T extends Comparable<? super T>> void sort(T[] array,
                                                              int fromIndex,
                                                              int toIndex) {
        int rangeLength = toIndex - fromIndex;
        int threads = Math.min(rangeLength / MINIMUM_THREAD_WORKLOAD,
                               Runtime.getRuntime().availableProcessors());

        threads = fixThreadCount(threads);

        if (threads < 2) {
            BottomUpMergesort.sort(array, fromIndex, toIndex);
            return;
        }

        int leftPartLength  = rangeLength >>> 1;
        int rightPartLength = rangeLength - leftPartLength;
        T[] aux = Arrays.copyOfRange(array, fromIndex, toIndex);

        SorterThread<T> thread1 = new SorterThread<>(threads >>> 1,
                                                     array,
                                                     aux,
                                                     fromIndex,
                                                     0,
                                                     leftPartLength);

        thread1.start();

        SorterThread<T> thread2 = new SorterThread<>(threads - threads >>> 1,
                                                     array,
                                                     aux,
                                                     fromIndex + leftPartLength,
                                                     leftPartLength,
                                                     rightPartLength);
        thread2.run();

        try {
            thread1.join();
        } catch (InterruptedException ex) {
            throw new IllegalStateException(
                    "A SorterThread threw an IllegalStateException.");
        }

        merge(aux, array, 0, fromIndex, leftPartLength, rightPartLength);
    }

    private static <T extends Comparable<? super T>> 
    void merge(T[] source,
               T[] target,
               int sourceOffset,
               int targetOffset,
               int leftRunLength,
               int rightRunLength) {
        int left  = sourceOffset;
        int leftUpperBound = sourceOffset + leftRunLength;
        int right = leftUpperBound;
        int rightUpperBound = leftUpperBound + rightRunLength;
        int targetIndex = targetOffset;

        while (left < leftUpperBound && right < rightUpperBound) {
            target[targetIndex++] =
                    source[right].compareTo(source[left]) < 0 ?
                    source[right++] :
                    source[left++];
        }

        System.arraycopy(source, 
                         left, 
                         target, 
                         targetIndex, 
                         leftUpperBound - left);

        System.arraycopy(source, 
                         right, 
                         target, 
                         targetIndex, 
                         rightUpperBound - right);
    }

    private static int fixThreadCount(int threads) {
        int ret = 1;

        while (ret < threads) {
            ret <<= 1;
        }

        return ret;
    }

    private static final class SorterThread<T extends Comparable<? super T>> 
    extends Thread {

        private final int threads;
        private final T[] source;
        private final T[] target;
        private final int sourceOffset;
        private final int targetOffset;
        private final int rangeLength;

        SorterThread(int threads,
                     T[] source,
                     T[] target,
                     int sourceOffset, 
                     int targetOffset,
                     int rangeLength) {
            this.threads = threads;
            this.source = source;
            this.target = target;
            this.sourceOffset = sourceOffset;
            this.targetOffset = targetOffset;
            this.rangeLength = rangeLength;
        }

        @Override
        public void run() {
            if (threads < 2) {
                BottomUpMergesort.sort(target,
                                       targetOffset,
                                       targetOffset + rangeLength);
                return;
            }

            int leftPartLength = rangeLength / 2;

            SorterThread<T> thread1 = new SorterThread<>(threads / 2,
                                                         target,
                                                         source,
                                                         targetOffset,
                                                         sourceOffset,
                                                         leftPartLength);

            thread1.start();

            SorterThread<T> thread2 = new SorterThread<>(
                                                threads - threads / 2,
                                                target, 
                                                source,
                                                targetOffset + leftPartLength,
                                                sourceOffset + leftPartLength,
                                                rangeLength - leftPartLength);

            thread2.run();

            try {
                thread1.join();
            } catch (InterruptedException ex) {
                throw new IllegalStateException(
                        "A SorterThread threw InterruptedException.");
            }

            merge(source, 
                  target, 
                  sourceOffset, 
                  targetOffset, 
                  leftPartLength,
                  rangeLength - leftPartLength);
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Implement a parallel version of a sorting algorithm (bubble sort or merge sort). Done in java...
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
  • TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL...

    TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL IT WITH 100 RANDOM INTEGERS, HAVING VALUES 0-99. PRINT THE UNSORTED ARRAY IMPLEMENT EACH SORTING ALGORITHM (DO NOT USE THE BUILT-IN LIBRARIES) FOR EACH ALGORITHM, INCLUDE PSEUDOCODE WITH NUMBERED STEPS. IN YOUR CODE, CLEARLY COMMENT WHICH STEP IS BEING PERFORMED BY THE LINE OR BLOCK OF CODE. USE A TIMER TO CHECK HOW LONG EACH ALGORITHM TAKES TO SORT THE ARRAY. THIS SHOULD BE...

  • write a python program Implement a “bouncing” bubble sort algorithm. In this version if bubble sort,...

    write a python program Implement a “bouncing” bubble sort algorithm. In this version if bubble sort, instead of making passes through a list that starts at the beginning andirons through to the end, you should reverse the direction each pass.That is , if the first pass starts at the beginning of the list and runs through to the end, the second pass out run from the end of the list back to the beginning and then the third pass would...

  • C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort...

    C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort • Merge Sort • Quick Sort Select any two of the above sorting techniques: 1. Write an algorithm (pseudocode) for the two selected sorting techniques 2. Write two separate C++ programs using user defined functions for each of the selected sorting techniques.

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below Languaga is C platform Ubuntu

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes....

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below Languaga is C platform Ubuntu

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

  • Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....

    Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java. Your program should receive its input from a file named "input.txt", which contains one integer per line. It should produce a sorted output file named "output.txt". Include a main method which demonstrates that your algorithm works.

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....

    Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort. Verify the correctness of each implemented algorithm by sorting the following array: 10, 5, 60, 53, 45, 3, 25,37,39,48

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