Javatpoint Logo
Javatpoint Logo

Merge Sort Using Multithreading in Java

Merge sort is a popular sorting algorithm that efficiently sorts an array or a list by dividing it into smaller sub-arrays sorting them independently and then merging them back together. It is renowned for its effectiveness, stability, and capacity for handling huge datasets. By using multithreading in Java, which enables us to divide the workload across numerous threads and execute them concurrently, we may improve the performance of merge sort.

Java's multithreading feature allows for the execution of several threads, each of which represents a separate flow of control, within a single programme. We may take advantage of the processing power available by using several threads to accelerate the sorting operation. Multithreaded merge sort's fundamental concept is to split the input array into smaller pieces and give each piece its own thread for sorting. Once the individual parts are sorted we merge them back together to obtain the final sorted array.

Example:

Input: {9, 2, 5, 1, 7, 4, 8, 3, 6, 11, 15, 12, 13, 10, 14}

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

ALGORITHM:

Step 1: Create a class MultithreadedMergeSort with private instance variables array and tempArray of type int[].

Step 2: Implement a public method sort(int[] inputArray) in the MultithreadedMergeSort class. This method will be responsible for initiating the sorting process.

Step 3: Inside the sort() method:

Step 3.1: Assign the inputArray to the array instance variable.

Step 3.2: Initialize the tempArray with the same length as inputArray.

Step 4: Implement a private method mergeSort(int low, int high) in the MultithreadedMergeSort class. This method will handle the actual sorting process using recursion.

Step 5: Inside the mergeSort() method:

Step 5.1: Check if low is less than high to ensure there is more than one element in the current sub-array.

Step 5.2: Calculate the mid index as the average of low and high.

Step 6: Create two separate threads using the Thread class and lambda expressions to sort the left and right halves of the sub-array:

Step 6.1: Create a leftThread that calls mergeSort() recursively for the left half, passing low and mid as arguments.

Step 6.2: Create a rightThread that calls mergeSort() recursively for the right half, passing mid + 1 and high as arguments.

Step 7: Start both threads using the start() method.

Step 8: Use the join() method on both threads to wait for their completion before proceeding.

Step 9: After the threads have completed, call the merge() method to merge the sorted halves.

Step 10: Implement a private method merge(int low, int mid, int high) in the MultithreadedMergeSort class. This method merges the two sorted halves back into the original array.

Step 11: Inside the merge() method:

Step 11.1: Copy both halves into the temporary array using System.arraycopy().

Step 11.2: Initialize leftIndex to low, rightIndex to mid + 1, and currentIndex to low.

Step 11.3: Compare elements from both halves and merge them back into the original array in ascending order.

Step 11.4: Copy any remaining elements from the left or right half, if any.

Step 12: In the main() method:

Step 12.1: Create an instance of MultithreadedMergeSort.

Step 12.2: Declare and initialize an input array.

Step 12.3: Call the sort() method on the MultithreadedMergeSort instance, passing the input array.

Step 12.4: Print the sorted array using Arrays.toString().

Implementation:

The implementation of the above steps are given below

FileName: MultithreadedMergeSort.java

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

Complexity Analysis:

The time complexity of merge sort using multithreading is the same as the regular merge sort algorithm, which is O(n log n), where "n" is the number of elements in the input array.

The space complexity of merge sort using multithreading is O(n), where "n" is the number of elements in the input array.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA