A Presentation On Merge Sort
A Presentation On Merge Sort
Submitted By :
Submitted To : Md.Nazmul Abdal
Student ID : 180225
Dr. Anupam Kumar Bairagi 2nd Year,2nd Term
Associate Professor Computer Science and Engineering
Computer Science and Engineering Discipline
Discipline Khulna University
Khulna University Khulna
Khulna
Merge Sort Algorithm
Merge Sort is a sorting algorithm. The algorithm divides the given list in two
halves, recursively sorts them and finally merges the two sorted halves.
The algorithm follows divide and conquer method.
At first, divide the n-element sequence to be sorted into two subsequences
of n/2 elements each.
Then sort the two subsequences recursively using merge sort.
After that merge the two sorted subsequences to produce the sorted
answer.
Divide And Conquer
Combine : Combining the solutions of the subproblems into one solution for
the original problem.
Determine Threshold : For which problem, the algorithm return directly result
without dividing to smaller problems
Divide-and-conquer Technique
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
Merge Sort Example
99 6 86 15 58 35 86 4 2
Merge Sort Example
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
Merge Sort Example
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
Merge Sort Example
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
Merge Sort Example
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
99 6 86 15 58 35 86 4 2
4 2
Merge Sort Example
99 6 86 15 58 35 86 2 4
Merge 4 2
Merge Sort Example
6 99 15 86 35 58 2 4 86
99 6 86 15 58 35 86 2 4
Merge
Merge Sort Example
6 15 86 99 2 4 35 58 86
6 99 15 86 35 58 2 4 86
Merge
Merge Sort Example
2 4 6 15 35 58 86 86 99
6 15 86 99 2 4 35 58 86
Merge
Merge Sort Example
2 4 6 15 35 58 86 86 99
Program
#include<iostream>
using namespace std;
int main()
{
int A[30],n,i;
cout << "Enter the Number of Elements : ";
cin >> n;
cout << "\nEnter the Numbers : ";
for (i=0; i<n; i++)
{
cin>>A[i];
}
mergesort(A,0,n-1);
cout << "\nAfter Sorting the Numbers are : ";
for (i=0; i<n; i++)
{
cout << A[i] << " ";
}
cout << endl;
return 0;
Program (continue)
void mergesort(int A[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
mergesort(A,first,mid);
mergesort(A,mid+1,last);
merge(A,first,mid,last);
}
}
{
int T[50],i,j,k,x;
i=first;
j=mid+1;
k=first;
Program (continue)
while(i<=mid && j<=last)
{
if(A[i]<=A[j])
T[k]=A[i++];
else
T[k]=A[j++];
k++;
}