Merge Sort Using Multithreading in Java7 Apr 2025 | 4 min read 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. |
The ideas of covariance and contravariance come to light in the complex world of Java programming as crucial building blocks for producing durable, flexible, and adjustable software. These ideas, which have their roots in the field of polymorphism, are crucial in determining how types and techniques...
5 min read
BreakIterator ious() method in Java with Examples The java.text.BreakIterator class consists of a ious() method. The current boundary is received by calling the current() method, while the index of the ious boundary behind it is obtained using the BreakIterator class. It gives the offset of the first...
3 min read
Java's basic data structure, HashMap, enables programmers to store and retrieve data effectively. Nesting of HashMap is a useful notion when working with complex data structures. In this section, we will discuss nested HashMap, its benefits, and implementation in applications. Comprehending and Applying a Map of...
5 min read
Java programming language requires variables to operate and handle data. Java creates several variables as per data format and data types. The variable declaration means creating a variable in a program for operating different information. The Java variable declaration creates a new variable with required properties....
5 min read
String compression is a fundamental problem in computer science and programming, where the objective is to compress a string by counting consecutive repeated characters. The problem's essence is to represent strings more efficiently, especially when dealing with large datasets. This technique is beneficial in various...
7 min read
Java, a versatile and widely-used programming language, employs various mechanisms for method dispatch, a process that determines which implementation of a method should be executed in response to a method call. Two primary methods of dispatch in Java are static dispatch and dynamic dispatch. Understanding the...
4 min read
In this game, stones are placed in a row (an input array is given). Two players are assigned the task of picking the stones of the maximum values. The player who collects stones of the maximum value wins the game. Player 1 will play first. After...
12 min read
? Trigonometry plays a crucial role in mathematics and various scientific applications, including computer graphics, physics, engineering, and more. In Java, we can easily find the trigonometric values of an angle using built-in math functions provided by the java.lang.Math class. In this section, we will discuss the...
4 min read
A super prime is a prime number that occupies a prime position in the sequence of all prime numbers. For example, in the list {2, 3, 5, 7, 11}, the second prime (3) and the third prime (5) are super primes. Identifying super primes involves both...
9 min read
A palindromic array reads the same backward as forward, similar to a palindromic string. Checking this involves comparing elements symmetrically from both ends. The Java program iterates through the array, verifying if the first and last elements, and so on, are equal, ensuring a simple and...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India