Merge Sort (Iteration)


/*********************************************************

-> This C++ program is to perform Merge sort
   using iterative method.

-> This program works in microsoft vc++ 6.0 environment.

-> The numbers are sorted in increasing order.

**********************************************************/

#include<iostream.h>
class sorting
{
private:
 double *array;
 int n;
 public:
 void input();
 void output();
 void mergesort();
};

void sorting::input()
{
 cout<<“****************************************************\n”
  <<“This program sorts numbers in increasing order”
  <<“\n\t\tusing Merge sort iterative technique\n”
  <<“****************************************************\n”;

 cout<<“Enter how many numbers you are going to enter for sorting ::”;
 cin>>n;
 array=new double[n];
 cout<<“Now enter your elements ::\n”;
 for(int i=0;i<n;i++)
  cin>>array[i];
}

void sorting::mergesort()
{
 double *temp=new double[n];
 int l1,l2,u1,u2,i;
 int size=1,j,k;
 while(size<n)
 {
          l1=0;
           k=0;
           while(l1+size<n)
           {
                    l2=l1+size;
                    u1=l2-1;
                    u2=(l2+size-1<n)?l2+size-1:n-1;
                    for(i=l1,j=l2;i<=u1&&j<=u2;k++)
                            if(array[i]<=array[j])
                            temp[k]=array[i++];
                            else
                            temp[k]=array[j++];
                    for(;i<=u1;k++)
                        temp[k]=array[i++];
                    for(;j<=u2;k++)
                        temp[k]=array[j++];
                    l1=u2+1;
           }
            for(i=l1;k<n;i++)
                temp[k++]=array[i];
            for(i=0;i<n;i++)
                array[i]=temp[i];

            size*=2;
 }
}

void sorting::output()
{
 cout<<“Now the sorted numbers are ::\n”;
 for(int i=0;i<n;i++)
  cout<<array[i]<<‘\t’;
 cout<<endl;
}

int main()
{
 sorting obj;
 obj.input();
 obj.mergesort();
 obj.output();
 return 0;
}
/**********************************************************************

****************************************************
This program sorts numbers in increasing order
                using Merge sort iterative technique
****************************************************
Enter how many numbers you are going to enter for sorting ::7
Now enter your elements ::
1.7
1.6
1.5
1.4
1.3
1.2
1.1
Now the sorted numbers are ::
1.1     1.2     1.3     1.4     1.5     1.6     1.7
Press any key to continue

************************************************************************/

7 thoughts on “Merge Sort (Iteration)”

  1. Really nice site you have here. IÃƒà ’Ã‚Â¢ÃƒÂ¢Ã¢â‚¬Å¡Ã‚Â¬ÃƒÂ¢Ã¢â‚¬Å¾Ã‚Â¢ve been reading for a while but this post made me want to say 2 thumbs up. Keep up the great work

  2. how come this program doesnt work in visual studio 2009?
    i mean what is iostream.h ? shouldnt it be ?
    and how can I get this to work in visual studio 2009
    it shows 92 errors my God! help haah
    can you get this to work in vs 2009 so I can see it run? thanks
    great program

  3. Pingback: e-like.ro
  4. Good day very nice site!! Guy .. Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also?I’m satisfied to search out a lot of useful information right here within the submit, we’d like develop extra techniques on this regard, thanks for sharing. . . . . .

  5. Howdy! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m
    not seeing very good success. If you know of any please share.
    Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s