Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Dept.

of CSIT
Dr. Parimal Kumar Giri
Experiment #3
Aim of the Experiment:
Write a program to perform Merge Sort for the given list of integer values.

Objective:
To familiarized with the implementation of Merge Sort Algorithm

Theory:
Merge sort is a divide-and-conquer sorting algorithm that divides an array into two halves,
sorts each half, and then merges them back together in a sorted fashion. The merge operation,
which combines two sorted arrays into a single sorted array, is the key step in the merge sort
algorithm. The algorithm does this by comparing the first element of each array and adding
the smaller of the two to the result array. This process is then repeated until both input arrays
have been fully merged. This process is repeated on each half of the array recursively until
the entire array is sorted.

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.
Algorithms:

Algorithm of Merge Sort:


Algorithm Mergesort(A[], low, high)
//A[low:high] is a global array to be sorted.
//Small(P) is true if there is only one element to sort. In this case the list is already
sorted.
{
If(low<high) then //if there are more than one element
{
//Divide P into subproblems. Find where to split the set.
mid:=(low + high)/2;
//Solve the subproblems.
Mergesort (A[], low,mid);
Mergesort (A[], mid+1,high);
//Combine the solution.
Merge(A[], low, mid, high);
}
}

Algorithm Merge()

Algorithm Merge(A[], low, mid, high)


//a[low:high] is a global array containing two sorted subsets in A[low: mid]
// and in A[mid+1:high].The goal is to merge these two sets into a single set
//residing in A[low:high]. C[] is an auxiliary global array.
{
k:=low;i:=low;j:=mid+1;
while((i<=mid) and (j<=high))
do { if(A[i]<=A[j]) then
{
C[k]:=A[i]; i:=i+1;
}
else
{C[k]:=A[j];j:=j+1; }k:=k+1;
}
if(i>mid)then
for m:=j to high
do {
C[k]:=a[m];k:=k+1;
}
else
for m:=i to mid
do {
C[k]:=a[i];i:=i+1;
}
for k:=low to high
do A[k]:=C[k];
}
Tools/Apparatus: Turbo C or gcc / gprof compiler in linux

Code:
C Programming Code

Output:
Copy and Paste Output Screen here

Discussion:
In this program merge sort algorithm is implemented where number 38, 27, 43, 3, 9, 82, and
10 are provided as a input and was sorted as 3,9,10,27, 38, 43, 82 in ascending order.

Time Complexity:
Merge Sort is a recursive algorithm and time complexity can be expressed as following
recurrence relation.

T(n) = 2T(n/2) + θ(n)

This implementation of merge sort is recursive and has a time complexity of O (n log n)
using Masters Method, making it more efficient than some other sorting algorithms for large
inputs.

Space Complexity:
• In merge sort all elements are copied into an auxiliary array
• So n auxiliary space is required for merge sort.
Auxiliary Space: O (n)

Conclusion:
Hence in this lab we are familiarized with the concept of merge sort algorithm

You might also like