Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

Merge Sort (Iteration)

Posted by Vinod on September 22, 2006


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

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

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

6 Responses to “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. 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

  3. [...] 7) Merge Sort (iterative) [...]

  4. Robert said

    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

  5. e-like.ro said

    e-like.ro…

    [...]Merge Sort (Iteration) « Data Structures through C & C++ for beginners[...]…

  6. Rick Otton said

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

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 )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 76 other followers