Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

THE FOLLOWING PROGRAM IS CALLED FORK JOIN SORTING. I DONT KNOW WHAT DOES IT MEAN AND WHAT DOES IT DO SO CAN ANYONE PLEASE EXPLAIN

THE FOLLOWING PROGRAM IS CALLED FORK JOIN SORTING.

I DONT KNOW WHAT DOES IT MEAN AND WHAT DOES IT DO SO CAN ANYONE PLEASE EXPLAIN TO ME THAT HOW CAN I USE IT WITH AN EXAMPLE, WHAT WILL BE THE OUTPUT AND WHAT WOULD THAT MEAN ?

package mergesortthread;

import java.util.logging.Logger; import java.util.logging.Level;

public class MyMergeSort {

// Variables to hold the input and temp arrays private int[] inputs; private int[] temp; private int inputLength;

/** * Sorts the input array using the merge sort algorithm * @param inputValues the input array to sort * @throws InterruptedException */ public void sort(int[] inputValues) throws InterruptedException { inputs = inputValues; inputLength = inputValues.length; temp = new int[inputLength]; mergeSort(0, inputLength - 1); }

/** * Recursive method that performs the merge sort algorithm * @param lo the lower index of the current subarray * @param hi the upper index of the current subarray * @throws InterruptedException */ private void mergeSort(int lo, int hi) throws InterruptedException { // Base case: if the subarray has only one element, it is already sorted if (lo >= hi) { return; }

// Calculate the middle index of the subarray int mid = lo + (hi - lo) / 2;

// Sort the left and right parts of the subarray in separate threads Thread tLeft = new Thread(new MergeSortThread(lo, mid, this)); Thread tRight = new Thread(new MergeSortThread(mid + 1, hi, this)); tLeft.start(); tRight.start(); tLeft.join(); tRight.join();

// Merge the two sorted parts of the subarray merge(lo, mid, hi); }

/** * Merges two sorted parts of the array * @param lo the lower index of the left part of the array * @param mid the upper index of the left part and the lower index of the right part of the array * @param hi the upper index of the right part of the array */ private void merge(int lo, int mid, int hi) { // Copy the elements of the input array to the temp array for (int i = lo; i <= hi; i++) { temp[i] = inputs[i]; }

// Initialize the indices for the left, right, and merged parts of the array int iLeft = lo; int iRight = mid + 1; int iMerged = lo;

// Merge the two parts of the array by comparing their elements while (iLeft <= mid && iRight <= hi) { if (temp[iLeft] <= temp[iRight]) { inputs[iMerged] = temp[iLeft]; iLeft++; } else { inputs[iMerged] = temp[iRight]; iRight++; } iMerged++; }

// Copy the remaining elements of the left part of the array while (iLeft <= mid) { inputs[iMerged] = temp[iLeft]; iMerged++; iLeft++; } }

/** * Main method to test the merge sort implementation * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { // Input array int[] array = new int[10];

// Fill the input array with descending values for (int pos = 0; pos < 10; pos++) { array[pos] = 10 - pos; }

// Measure the time taken to sort the array long inputStart = System.currentTimeMillis();

// Sort the array new MyMergeSort().sort(array);

long inputEnd = System.currentTimeMillis();

// Print the sorted array for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); }

// Print the time taken to sort the array System.out.println(" Time taken: " + (inputEnd - inputStart) + "ms"); } }

class MergeSortThread implements Runnable {

// Variables to hold the lower and upper indices of the subarray private final int first; private final int last; private MyMergeSort myMergeSort;

/** * Constructor * @param f the lower index of the subarray * @param l the upper index of the subarray */ public MergeSortThread(int f, int l, MyMergeSort m) { this.first = f; this.last = l; this.myMergeSort = m; }

/** * The method to run when the thread is started */ @Override public void run() { try { myMergeSort.mergeSort(first, last); } catch (InterruptedException ieEx) { Logger.getLogger(MergeSortThread.class.getName()).log(Level.SEVERE, null, ieEx); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions