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
************************************************************************/
Total Natural Nurtition said
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
Computer Information Plus said
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
index for c++ programs to implement sorting techniques « Data Structures through C & C++ for beginners said
[...] 7) Merge Sort (iterative) [...]
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
e-like.ro said
e-like.ro…
[...]Merge Sort (Iteration) « Data Structures through C & C++ for beginners[...]…
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. . . . . .