0% found this document useful (0 votes)
20 views4 pages

Merge Sort 80

Uploaded by

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

Merge Sort 80

Uploaded by

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

Name: Mohini Vilas Narkhede

Roll no.: 80
Practical no.:
Title: Implementation Of Programs for Merge Sort Technique.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class LIST
{
int *A,n;
public:
LIST(int);
void READ_LIST_80();
void SHOW_LIST_80();
void MERGE_SORT_ASC_80(int,int);
void MERGE_SORT_DESC_80(int,int);
void MERGE_ASC_80(int,int,int);
void MERGE_DESC_80(int,int,int);
};
LIST::LIST(int par)
{
n=par;
A=new int[n+1];
}
void LIST::READ_LIST_80()
{
cout<<"\n Enter the list Elements: ";
for(int i=1;i<=n;i++)
cin>>A[i];
}
void LIST::SHOW_LIST_80()
{
for(int i=1;i<=n;i++)
cout<<" "<<A[i];
}
void LIST::MERGE_SORT_ASC_80(int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
MERGE_SORT_ASC_80(low,mid);
MERGE_SORT_ASC_80(mid+1,high);
MERGE_ASC_80(low,mid,high);
}
}
void LIST::MERGE_SORT_DESC_80(int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
MERGE_SORT_DESC_80(low,mid);
MERGE_SORT_DESC_80(mid+1,high);
MERGE_DESC_80(low,mid,high);
}
}
void LIST::MERGE_ASC_80(int low,int mid,int high)
{
int *B=new int[n+1];
int h=low,i=low,j=mid+1;
while(h<=mid && j<=high)
{
if(A[h] < A[j])
{
B[i]=A[h];
h=h+1;
}
else
{
B[i]=A[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
while(j<=high)
{
B[i]=A[j];
j=j+1;
i=i+1;
}
}
else
{
while(h<=mid)
{
B[i]=A[h];
h=h+1;
i=i+1;
}
}
for(i=low;i<=high;i++)
A[i]=B[i];
}
void LIST::MERGE_DESC_80(int low,int mid,int high)
{
int *B=new int[n+1];
int h=low,i=low,j=mid+1;
while(h<=mid && j<=high)
{
if(A[h] > A[j])
{
B[i]=A[h];
h=h+1;
}
else
{
B[i]=A[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
while(j<=high)
{
B[i]=A[j];
j=j+1;
i=i+1;
}
}
else
{
while(h<=mid)
{
B[i]=A[h];
h=h+1;
i=i+1;
}
}
for(i=low;i<=high;i++)
A[i]=B[i];
}
void main()
{
int no;
clrscr();
cout<<"\n Enter the size of List: ";
cin>>no;
LIST obj(no);
obj.READ_LIST_80();
cout<<"\n List Elements before sorting: ";
obj.SHOW_LIST_80();
obj.MERGE_SORT_ASC_80(1,no);
cout<<endl<<"\n Elements sorting in Ascending: ";
obj.SHOW_LIST_80();
obj.MERGE_SORT_DESC_80(1,no);
cout<<endl<<"\n Elements sorting in Descending: ";
obj.SHOW_LIST_80();
cout<<"\n List Elements After sorting: ";
obj.SHOW_LIST_80();
getch();
}

You might also like