Mini Project
Mini Project
Title:
Implement merge sort and multithreaded merge sort. Compare time required by
both the algorithms. Also analyze the performance of each algorithm for the
best case and the worst case.
Objective:
Compare time required and analyse performance of each algorithm for the best
case and the worst case.
Prerequisite:
a) Basic of Java Programming
b) Concept of sorting algorithms and time complexity
Theory:
Merge Sort:
Merge sort is a highly effective sorting algorithm that follows the divide-and-
conquer approach. The method involves recursively dividing an unsorted list
into smaller sub-lists, each with one element, and then progressively merging
these sub-lists to create sorted sub-lists. One notable feature of merge sort is its
stability, meaning it preserves the relative order of equal elements during the
sorting process. The time complexity of merge sort is (O(n log n)), making it a
preferred choice for large datasets. However, it comes with a space complexity
of (O(n)) due to the additional space required for the merging process.
Multithreaded Sorting:
Multithreaded sorting algorithms aim to enhance sorting performance by
capitalizing on parallelism. In this approach, the sorting task is divided among
multiple threads that can execute concurrently. Efficient coordination and
synchronization between threads are critical to ensuring the correct sorting of
data. Load balancing becomes a key consideration to distribute work evenly
among threads, preventing idle times or overloading. Multithreaded sorting is
particularly advantageous in systems with multiple processors or cores, where it
can significantly improve scalability. However, challenges such as
synchronization, race conditions, and deadlocks must be carefully addressed in
the implementation to achieve optimal results.
Performance Comparison:
> In terms of time complexity, both standard merge sort and multithreaded
merge sort have the same time complexity for best, worst, and average cases.
> The performance analysis for both algorithms is the same because they both
have the same time complexity. In the best and worst cases, they exhibit O(n log
n) time complexity.
> However, the multithreaded merge sort can potentially take advantage of
multiple CPU cores on a multi-core machine, providing better performance for
large input arrays due to parallelism. This can be especially useful when sorting
very large datasets.
> In summary, the multithreaded merge sort is a parallel version of the standard
merge sort and can provide improved performance for large datasets on multi-
core processors.
> It can take advantage of parallelism to sort the array faster, but there is an
overhead associated with creating and managing threads, so for small arrays, it
might be slower than the standard merge sort.
MERGE -SORT
// ------------------------------------------------------------------------------
// Merge Sort
// ------------------------------------------------------------------------------
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}
System.out.println("----------------------------------------------");
System.out.println();
System.out.print("----------------------------------------------");
System.out.println("\nSorted Array:");
for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
System.out.println("----------------------------------------------");
}
}
OUTPUT:
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}
@Override
protected void compute() {
if (left < right) {
int mid = left + (right - left) / 2;
MergeSortTask leftTask = new MergeSortTask(arr, left, mid);
MergeSortTask rightTask = new MergeSortTask(arr, mid + 1, right);
System.out.println("----------------------------------------------");
System.out.println("Unsorted Array:");
for (int value : arr) {
System.out.print(value + " ");
}
parallelMergeSort(arr);
System.out.println();
System.out.print("----------------------------------------------");
System.out.println("\nSorted Array:");
for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
System.out.println("----------------------------------------------");
}
}
OUTPUT: