Dsu Partb
Dsu Partb
GROUP NO: 04
GROUP MEMBERS:
Roll no Name of Students Enrollment no
1
This Is To Certify That Mr. /Ms. Gargi Garad, Karan Karvekar, Prasad Pilke, Shivam Raut and Nikhil Chaudhari
group of Diploma in Information Technology. Pimpri Chinchwad Polytechnic (Code: 0056) has
completed Micro Project of the course as prescribed in the curriculum for the academic year 2023-2024.
Date:
Ms.Madhavi Mali
2
1. INTRODUCTION :-
Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves,
calls itself for the two halves, and then it merges the two sorted halves. The merge()function
is used for merging two halves. The sub-lists are divided again and again into halves until
the list cannot be divided further. Then we combine the pair of one element lists into two-
element lists, sorting them in the process. The sorted two-element pairs is merged into the
four-element lists, and so on until we get the sorted list.
Time Complexity: O(N log(N)), Merge Sort is a recursive algorithm and time complexity
can be expressed as following recurrence relation.
Auxiliary Space: O(N), In merge sort all elements are copied into an auxiliary array. So N
auxiliary space is required for merge sort.
2. AIM :-
To sort array elements by using merg sort algorithm.
3
5. RESOURCES / MATERIAL REQUIRED :-
3 Software DevC++
6. REPORT :-
i. Introduction:-
It is one of the most popular sorting algorithms used in computer science, and it is
fundamental to understand for both freshers and professionals alike. In this article,
we will give you a rundown of the basics of merge sort, from analyzing complexity
to understanding advantages and disadvantages. We will also provide a visual
example and explain the pseudocode involved in the process.
At its core, merge sort operates using a divide and conquer strategy. This means that
it takes a large dataset and splits it into smaller pieces so that they can be sorted
more easily. To achieve this, divide array recursively until individual elements are
formed. Once these individual elements are formed, they can then be merged
together in sorted subarrays which eventually result in a completely sorted array.
When analyzing the complexity of merge sort, it follows an O(n log n) process
meaning that as the size of datasets increase, so too does its running time
accordingly. As such, in relation to other sorting algorithms (e.g., bubble sort or
insertion sort), it is considered one of the faster sorting algorithms out there. Despite
its efficiency however, there are certain disadvantages to bear in mind when using
merge sort such as its storage requirements (it requires a large amount of extra space
due to splitting) and its inability to adapt itself for partially sorted data sets (the
entire list must be split each time).
4
ii. Algorithm Of Merg Sort :-
Step 1:Start
Step 2:Declare an array of MAX elements
Step 3: Declare mergSort function with one integre array & 2 integre as parameter
Step 4: Declare merge function with 1 integre array & 3 integer as parameter
Step 5: Aceept the unsorted array in declared array.
Step 6: Call mergeSort function.
Step 7: Display sorted array.
Step 8: Stop.
iii. Flowchart:-
5
iv. Program To Implement Merge Sort:-
/*Program for sorting array elements using merg & sort algorithm*/
#include<stdio.h>
#include<conio.h>
#define MAX 30
int main()
{
int data[MAX];
int i,n;
int lower,upper;
lower=0;
upper=n-1;
6
{
printf("\n Enter %d element:",i+1);
scanf("%d",&data[i]);
}
mergeSort(data,0,4);
getch();
return 0;
}
void mergeSort(int data[],int lower,int upper)
{
7
int mid;
if(lower<upper)
{
mid=(lower+upper)/2;
mergeSort(data,lower,mid);
mergeSort(data,mid+1,upper);
merge(data,lower,mid,upper);
}
}
void merge(int data[],int lower,int mid,int upper)
{
int c[MAX];
int i,j,k;
i= lower;
j= mid+1;
k=0;
8
else
{
c[k]=data[j];
k++;
j++;
}
}
while(i<=mid)
{
c[k]=data[i];
i++;
k++;
}
while(j<=upper)
{
c[k]=data[j];
k++;
j++;
}
for(i=lower,j=0;i<=upper;i++,j++)
{
data[i]=c[j];
}
}
9
v. Output of Program
10
vi. Diagramic Representation
11
12
13
14
15
16
17
vii. Overview of Merge Sort Algorithm
Merge sort algorithm is a key concept in computer science which helps to sort elements
of an array. It falls under the category of comparison based sorting, or divide and conquer
algorithms, using recursive calls to divide a given array into two halves until the
subarrays consist of single elements. This sorting method has logarithmic complexity and
is considered relatively efficient and stable.
It involves repeatedly splitting a list or array into two halves, sorting each half and then
merging the sorted halves back together. By breaking down the data into smaller chunks,
merge sort can quickly and efficiently sort any input data set.
viii. Conclusion
It is a divide and conquer method of sorting. Merge sort is a sorting where best,worst
and average case time complexities are the same.
Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires
additional memory to store the sorted data. This can be a disadvantage in
applications where memory usage is a concern.
Not always optimal for small datasets: For small datasets, Merge sort has a higher time
complexity than some other sorting algorithms, such as insertion sort. This can result
in slower performance for very small datasets.
18
xi. Applications of Merge Sort
Sorting large datasets: Merge sort is particularly well-suited for sorting large
datasets due to its guaranteed worst-case time complexity of O(n log n).
External sorting: Merge sort is commonly used in external sorting, where the data to
be sorted is too large to fit into memory.
Custom sorting: Merge sort can be adapted to handle different input distributions,
such as partially sorted, nearly sorted, or completely unsorted data.
19
7. Skill Developed / learning out of this Micro-Project:-
4 Skill of Leadership ✔
6 Skill of Planning ✔
9 Skill of Creativity ✔
20