/* Write C++ programs for sorting a given list of elements in ascending order using Merge sort methods */
#include<iostream> #include<conio.h> using namespace std; void mergesort(int *,int,int); void merge(int *,int,int,int); int a[20],i,n,b[20]; main() { cout <<"\n enter no of elements"; cin >> n; cout <<"enter the elements"; for(i=0;i<n;i++) cin >> a[i]; mergesort(a,0,n-1); cout <<" numbers after sort"; for(i=0;i<n;i++) cout << a[i] << " "; getch(); } void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); mergesort(a,mid+1,j); merge(a,i,mid,j); } } void merge(int a[],int low,int mid ,int high) { int h,i,j,k; h=low; i=low; j=mid+1; while(h<=mid && j<=high) { if(a[h]<=a[j]) b[i]=a[h++]; else b[i]=a[j++]; i++; } if( h > mid) for(k=j;k<=high;k++) b[i++]=a[k]; else for(k=h;k<=mid;k++) b[i++]=a[k]; cout <<"\n"; for(k=low;k<=high;k++) { a[k]=b[k]; cout << a[k] <<" "; } } |
OUTPUT
N enter no of elements8 12 5 61 60 50 1 70 81
enter the elements
5 12
60 61
5 12 60 61
1 50
70 81
1 50 70 81
1 5 12 50 60 61 70 81 numbers after sort1 5 12 50 60 61 70 81
lots of error occur when we compile the program
it s very usefull prg
can someone explain the part of the wherein there are two the same functions inside the very own function? the mergesort function
6% in champaign shoes 2012, at White Rock Lake. This may allow for fluid motion. Our corporate tax rate was 30 and 40 stores
opened during the 2014 FIFA World Cup football match between Chile
and Australia at Arena de Sao Paulo, Brazil.
My spouse and I absolutely love your blog and find the majority of your post’s to be precisely what I’m looking for.
can you offer guest writers to write content available
for you? I wouldn’t mind composing a post or elaborating on some of the subjects you write about here.
Again, awesome weblog!
In Hotline Miami 2, the thought that there are numerous techniques for the story to be interpreted remains true.
That becoming said, he’s being viewed in a quantity of
ways, and diverse characters and factions are seen reacting to that.
Hello, I tink your site might be having browser compatibiility issues.
When I look at your blog in Opera, it looks fine but when opening
in Internet Explorer, it has some overlapping.
I just wanted to gve you a quick heads up! Other then that, fantastic blog!
Do you mind if I quote a couple of your articles as long as I
provide credit and sources back to your webpage?
My website is in the very same niche as yours and my users would genuinely benefit from some of the information you provide here.
Please let me know if this ok with you. Thanks!
Cc moi je peut vous donner des pas et des équss automobile je quitte equideow il vous suffi juste
d’envoyer votre mdp et pseudo a drogould sur equideow bonne journée Vous voulez hacker equideow alors regardé ma vidéo
Equus Generator 2014 v1.” Bientôt le jeu Equideow n’aura plus aucune valeur !
Register to Electrofrinds Member Login
Home
Articles
Interview Questions
Projects
Question and Answer
Source Codes
Seminar Resources
studentprojects.in > Source Codes > Software Programs > C++ Programs > Basic Programs > C++ programs for sorting a given numbers in ascending order using Merge sort
C++ programs for sorting a given numbers in ascending order using Merge sort
Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone
/* Write C++ programs for sorting a given list of elements in ascending order using Merge sort methods */
#include
#include
using namespace std;
void mergesort(int *,int,int);
void merge(int *,int,int,int);
int a[20],i,n,b[20];
main()
{
cout <> n;
cout <<"enter the elements";
for(i=0;i> a[i];
mergesort(a,0,n-1);
cout <<" numbers after sort";
for(i=0;i<n;i++)
cout << a[i] << " ";
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,j);
}
}
void merge(int a[],int low,int mid ,int high)
{
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while(h<=mid && j<=high)
{
if(a[h] mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
cout <<"\n";
for(k=low;k<=high;k++)
{ a[k]=b[k];
cout << a[k] <<" ";
}
}