Implementation of
Merge Sort
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
Experiment No. 1
Aim: Implementation of Merge sort using divide and conquer approach.
Required Software/ Software Tool:
- Windows Operating System
-C/C++/JAVA
Theory:
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time
complexity being Ο (n log n), it is one of the most respected algorithms. Merge sort first divides
the array into equal halves and then combines them in a sorted manner.
How Merge Sort Works?
To understand merge sort, we take an unsorted array as the
following −
We know that merge sort first divides the whole array iteratively into equal halves unless
the atomic values are achieved. We see here that an array of 8 items is divided into two
arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we
divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be
divided.
Now, we combine them in exactly the same manner as they were broken down. Please
note the color codes given to these lists.
We first compare the element for each list and then combine them into another list in a
sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
and in the target list of 2 values we put 10 first, followed by 27. We change the order of
19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and
merge them into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Now we should learn some programming aspects of merge sorting.
Algorithm:
Merge sort keeps on dividing the list into equal halves until it can no longer be divided.
By definition, If it is only one element in the list, it is sorted. Then, merge sort combines
the smaller sorted lists keeping the new list sorted too.
Step 1 − If it is only one element in the list it is already sorted,
return.
Step 2 − divide the list recursively into two halves until it can no
more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Implementing the Solution Writing Source Code:
//Tanishka Mayekar
// Merge Sort
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int n;
int i,low,high;
void main()
{
int A[10];
void MergeSort(int A[10],int low,int high);
void Display(int A[10]);
printf("\n\t\t Merge Sort \n");
printf("\n Enter the length of list: ");
scanf("%d",&n);
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
printf("\n Enter list elements: ");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
low=0;high=n-1;
MergeSort(A,low,high);
Display(A);
getch();
}
void MergeSort(int A[10],int low,int high)
{
int mid;
void Combine(int A[10],int low, int mid,int high);
if(low<high){
mid=(low+high)/2;
MergeSort(A,low,mid);
MergeSort(A,mid+1,high);
Combine(A,low,mid,high);
}
}
void Combine(int A[10],int low,int mid,int high)
{
int i,j,k;
int temp[10];
k=low;
i=low;
j=mid+1;
while(i<=mid && j<=high){
if(A[i]<=A[j]){
temp[k]=A[i];
i++;
k++;
}
else{
temp[k]=A[j];
j++;
k++;
}
}
while(i<=mid){
temp[k]=A[i];
i++;
k++;
}
while(j<=high){
temp[k]=A[j];
j++;
k++;
}
for(k=low;k<=high;k++)
A[k]=temp[k];
}
void Display(int A[10])
{
int i;
printf("\n\n The Sorted Array is: \n");
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
for(i=0;i<n;i++)
printf("%d\t",A[i]);
}
Input and Output:
Mahavir Education Trust's
SHAH & ANCHOR KUTCHHI ENGINEERING COLLEGE
Chembur, Mumbai - 400 088
UG Program in Electronics & Computer Science
Conclusion:
In this experiment we have implemented Merge sort algorithm using Divide and Conquer
approach.