C++ programs for sorting a given numbers in ascending order using Merge sort

/* 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

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

10 thoughts on “C++ programs for sorting a given numbers in ascending order using Merge sort

  1. can someone explain the part of the wherein there are two the same functions inside the very own function? the mergesort function

  2. 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.

  3. 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!

  4. 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.

  5. 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!

  6. 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!

  7. 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 !

  8. 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] <<" ";
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in