0% found this document useful (0 votes)
106 views19 pages

A Presentation On Merge Sort

This document presents a merge sort algorithm. It divides the input list into halves recursively, sorts the halves, and then merges the sorted halves back together. It follows the divide and conquer approach of dividing the problem into subproblems, solving those subproblems recursively, and then combining the solutions. Pseudocode and an example are provided to demonstrate how merge sort works on sample input.

Uploaded by

mna shourov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views19 pages

A Presentation On Merge Sort

This document presents a merge sort algorithm. It divides the input list into halves recursively, sorts the halves, and then merges the sorted halves back together. It follows the divide and conquer approach of dividing the problem into subproblems, solving those subproblems recursively, and then combining the solutions. Pseudocode and an example are provided to demonstrate how merge sort works on sample input.

Uploaded by

mna shourov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

A Presentation on Merge Sort

Course Name : Algorithms Laboratory


Course Title : CSE-2202

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

 Divide : Division of the problem into a number of subproblems that are


smaller instances of the same problem.

 Conquer : Conquering the subproblems by solving them recursively. If they


are small enough, solve the subproblems as base cases.

 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;

void mergesort(int A[],int first,int last);


void merge(int A[],int first,int mid,int last);

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);
}
}

void merge(int A[],int first,int mid ,int 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++;
}

if( i > mid)


{
for(x=j; x<=last; x++)
{
T[k++]=A[x];
}
}
else
{
for(x=i; x<=mid; x++)
{
T[k++]=A[x];
}
}
Program (continue)
for(x=first; x<=last; x++)
{
A[x]=T[x];
}
}

Sample Input and Output : (Using 20 Nodes)


Enter the Number of Elements : 20

Enter the Numbers : 10 52 33 92 54 22 30 25 62 3 57 28 32 49 73 16 19 37


59 68

After Sorting the Numbers are : 3 10 16 19 22 25 28 30 32 33 37 49 52 54


57 59 62 68 73 92

You might also like