Implementing a multi-threaded Quick Sort in Java with the option to change the number of threads can be a nice exercise in understanding concurrency and recursive sorting algorithms. Here's a simple version of such a multi-threaded Quick Sort where the maximum number of threads can be adjusted:
Please note that for actual production systems, the Java Fork/Join framework or a ThreadPool would be a more robust choice for multi-threading tasks like this. For educational purposes, I will provide you with a simple Java example using bare threads.
import java.util.Arrays;
public class MultiThreadedQuickSort {
// Maximum number of threads to be used
private static final int MAX_THREADS = Runtime.getRuntime().availableProcessors();
private static class QuickSortThread extends Thread {
private int[] arr;
private int low;
private int high;
private static int threadCount = 0;
QuickSortThread(int[] arr, int low, int high) {
this.arr = arr;
this.low = low;
this.high = high;
}
@Override
public void run() {
if (high <= low) {
return;
}
int pivotIdx = partition(arr, low, high);
QuickSortThread left = null, right = null;
if (threadCount < MAX_THREADS) {
synchronized (QuickSortThread.class) {
if (threadCount < MAX_THREADS) {
left = new QuickSortThread(arr, low, pivotIdx - 1);
threadCount++;
left.start();
}
}
}
if (threadCount < MAX_THREADS) {
synchronized (QuickSortThread.class) {
if (threadCount < MAX_THREADS) {
right = new QuickSortThread(arr, pivotIdx + 1, high);
threadCount++;
right.start();
}
}
}
if (left == null) {
quickSort(arr, low, pivotIdx - 1);
} else {
try {
left.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (right == null) {
quickSort(arr, pivotIdx + 1, high);
} else {
try {
right.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (QuickSortThread.class) {
threadCount--;
}
}
private int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
private void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIdx = partition(arr, low, high);
quickSort(arr, low, pivotIdx - 1);
quickSort(arr, pivotIdx + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}
private static void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static void main(String[] args) {
int[] array = {24, 2, 45, 20, 56, 75, 2, 56, 99, 53, 12};
QuickSortThread quickSort = new QuickSortThread(array, 0, array.length - 1);
quickSort.start();
try {
quickSort.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Sorted array: " + Arrays.toString(array));
}
}
The MultiThreadedQuickSort program initializes a Quick Sort implemented in a multi-threaded manner. Please note that creating a new thread for each recursive call is not efficient for larger thresholds of data; hence this implementation limits the number of threads by using a static counter threadCount.
In the run() method of the QuickSortThread inner class, the left and right partitioned sub-arrays are processed in separate threads if the current threadCount is less than the MAX_THREADS. If not, the quick sort will proceed in a single-threaded manner.
The partition() and swap() methods are self-contained utility functions for the Quick Sort algorithm.
The main() method shows how to start the sorting process by creating an instance of QuickSortThread and waits for it to finish before it prints out the sorted array.