DAA Mini Project Report Atul
DAA Mini Project Report Atul
Bachelor in
COMPUTER ENGINEERING
Nashik – 422003.
Certificate
Every work is source which requires support from many people and areas. It
gives us proud privilege to complete the Design And Analysis of Algorithms Mini Project
on “Implement merge sort and multithreaded merge sort. Compare time required by
both the algorithms. Also analyse the performance of each algorithm for the best case
and the worst case” under valuable guidance and encouragement of our guide Prof.
V.P.Wani.
We are also extremely grateful to our respected H.O.D. Dr. M. U. Kharat for
providing all facilities and every help for smooth progress of our Mini Project. At last we
would like to thank all the staff members and our students who directly or indirectly
supported me without which the Mini Project work would not have been completed
successfully.
by
1. Introduction
2. Problem Statement
3. Objectives
4. Working
5. Algorithms
6. Merge Sort Using Multi-Threading
7. Code
8. Conclusions
1
Introduction
The project titled "Design And Analysis of Algorithms: Implementing Merge Sort and
Multithreaded Merge Sort" delves into the realm of sorting algorithms, specifically focusing on
Merge Sort and its enhanced variant, Multithreaded Merge Sort. Sorting is a fundamental
operation in computer science, and this project aims to explore the efficiency and performance
of these sorting algorithms.
2. Comparative Analysis:
The project doesn't stop at mere implementation but delves into a thorough comparative
analysis. It measures and contrasts the time required by the two sorting algorithms. This analysis
is crucial in determining which algorithm performs better in different scenarios and can be the
basis for algorithm selection in practical applications.
2
Problem Statement
Implement merge sort and multithreaded merge sort. Compare time required by both
the algorithms. Also analyse the performance of each algorithm for the best case and the
worst case.
3
Objectives
1. Implementation of Merge Sort and Multithreaded Merge Sort: The primary objective
is to implement the Merge Sort algorithm and its multithreaded variant, Multithreaded Merge
Sort, to gain a deep understanding of these sorting techniques.
2. Time Comparison: Compare the execution time of the two sorting algorithms—Merge
Sort and Multithreaded Merge Sort—under various scenarios to determine which one is more
efficient and under what conditions.
3. Performance Analysis - Best Case: Analyze the performance of both algorithms in the
best-case scenario to understand their capabilities when dealing with already sorted or nearly
sorted data.
4. Performance Analysis - Worst Case: Examine the performance of the sorting algorithms
in the worst-case scenario, where the input data is in reverse order, to identify their limitations
and inefficiencies.
4
Working
The Merge Sort algorithm is a sorting algorithm that is based on the Divide and Conquer
paradigm. In this algorithm, the array is initially divided into two equal halves and then they
are combined in a sorted manner.
To know the functioning of merge sort, lets consider an array arr[] = {38, 27, 43, 3, 9,
82, 10}
• At first, check if the left index of array is less than the right index, if yes then calculate
its mid point
• Now, as we already know that merge sort first divides the whole array iteratively into
equal halves, unless the atomic values are achieved.
• Here, we see that an array of 7 items is divided into two arrays of size 4 and 3
respectively.
5
• Now, again find that is left index is less than the right index for both arrays, if found
yes, then again calculate mid points for both the arrays.
• Now, further divide these two arrays into further halves, until the atomic units of the
array is reached and further division is not possible.
• After dividing the array into smallest units, start merging the elements again based on
comparison of size of elements
• Firstly, compare the element for each list and then combine them into another list in a
sorted manner.
6
The following diagram shows the complete merge sort process for an example array
{38, 27, 43, 3, 9, 82, 10}.
If we take a closer look at the diagram, we can see that the array is recursively divided
into two halves till the size becomes 1. Once the size becomes 1, the merge processes come
into action and start merging arrays back till the complete array is merged.
7
Algorithm
step 1: start
step 4: Stop
Follow the steps below the solve the problem:
MergeSort(arr[],l,r)
If r > l
• Find the middle point to divide the array into two halves:
• middle m = l + (r – l)/2
• Call mergeSort(arr, l, m)
• Call mergeSort(arr, m + 1, r)
• Call merge(arr, l, m, r)
8
Time Complexity: O(N log(N)
Sorting arrays on different machines. Merge Sort is a recursive algorithm and time complexity
can be expressed as following recurrence relation.
9
Merge Sort Using Multi-Threading
Merge Sort is a popular sorting technique which divides an array or list into two halves
and then start merging them when sufficient depth is reached. Time complexity of merge sort
is O(nlogn).
Threads are lightweight processes and threads shares with other threads their code
section, data section and OS resources like open files and signals. But, like process, a thread
has its own program counter (PC), a register set, and a stack space.
Examples:
Input : 83, 86, 77, 15, 93, 35, 86, 92, 49, 21, 62, 27, 90, 59, 63, 26, 40, 26, 72, 36
Output : 15, 21, 26, 26, 27, 35, 36, 40, 49, 59, 62, 63, 72, 77, 83, 86, 86, 90, 92, 93
10
Code
CPP Code for both Simple Merge Sort and Merge Sort Using Multi-Threading
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
int i = 0, j = 0, k = left;
11
arr[k] = R[j];
j++;
k++;
}
}
leftThread.join();
rightThread.join();
int main() {
std::vector<int> arr = {83, 86, 77, 15, 93, 35, 86, 92, 49, 21, 62, 27, 90, 59, 63, 26, 40,
26, 72, 36};
int arrSize = arr.size();
12
for (int num : arr)
std::cout << num << " ";
std::cout << std::endl;
std::cout << "Time taken by Simple Merge Sort: " << durationSimple.count() << "
seconds" << std::endl;
arr = {83, 86, 77, 15, 93, 35, 86, 92, 49, 21, 62, 27, 90, 59, 63, 26, 40, 26, 72, 36 };
return 0;
}
Output
13
Conclusion
Hence, Here we Implement merge sort and multithreaded merge sort. By Comparing time
required by both the algorithms merge sort and multithreaded merge sort. We analyze the
performance of each algorithm for the best case and the worst case.
14