// MERGE SORT
# include<iostream.h>
class merge_s
{
public:
public:
void merge_sort(float *, int , int , int );
void merge_pass(float *, int , int );
};
// definition of the function
void merge_s :: merge_sort(float l[], int top, int size, int bottom)
{
float temp[1000];
int f = top;
int s = size +1 ;
int t = top ;
while(( f <= size)&&(s <=bottom))
{ if(l[f] <= l[s])
{
temp[t] = l[f] ;
f ++;
}
else
{
temp[t] = l[s] ;
s ++;
}
t ++;
}
if(f <=size)
{
for(f = f ; f<=size; f++)
{
temp[t] = l[f];
t++;
}
}
else
{
for(s = s ; s<= bottom ; s++)
{
temp[t]=l[s];
t++;
}
}
for( int upper = top; upper <= bottom ; upper++)
{
l[upper]=temp[upper];
}
}
// definition of the function
void merge_s :: merge_pass( float append[], int m, int n )
{
if( m!= n)
{
int mid = (m+n)/2;
merge_pass( append, m, mid );
merge_pass( append, mid+1, n );
merge_sort( append, m, mid, n );
}
}
// main function
void main()
{
merge_s sort;
float list[1000];
int n;
cout<<"\n Size of the list :";
cin>> n;
for(int i = 0 ; i<n ; i++)
{
cout<<"\n Elements of the list : "<< i+1 <<" : ";
cin>>list[i];
}
cout << "\n Entered list as follows:\n";
for( i = 0 ; i<n ; i++)
cout <<" "<< list[i];
i = 0 ;
sort.merge_pass(list, i, n-1);
cout<<"\n Merge sorted list is as follows:\n";
for( i = 0 ; i<n ; i++)
cout <<" "<< list[i];
}
# include<iostream.h>
class merge_s
{
public:
public:
void merge_sort(float *, int , int , int );
void merge_pass(float *, int , int );
};
// definition of the function
void merge_s :: merge_sort(float l[], int top, int size, int bottom)
{
float temp[1000];
int f = top;
int s = size +1 ;
int t = top ;
while(( f <= size)&&(s <=bottom))
{ if(l[f] <= l[s])
{
temp[t] = l[f] ;
f ++;
}
else
{
temp[t] = l[s] ;
s ++;
}
t ++;
}
if(f <=size)
{
for(f = f ; f<=size; f++)
{
temp[t] = l[f];
t++;
}
}
else
{
for(s = s ; s<= bottom ; s++)
{
temp[t]=l[s];
t++;
}
}
for( int upper = top; upper <= bottom ; upper++)
{
l[upper]=temp[upper];
}
}
// definition of the function
void merge_s :: merge_pass( float append[], int m, int n )
{
if( m!= n)
{
int mid = (m+n)/2;
merge_pass( append, m, mid );
merge_pass( append, mid+1, n );
merge_sort( append, m, mid, n );
}
}
// main function
void main()
{
merge_s sort;
float list[1000];
int n;
cout<<"\n Size of the list :";
cin>> n;
for(int i = 0 ; i<n ; i++)
{
cout<<"\n Elements of the list : "<< i+1 <<" : ";
cin>>list[i];
}
cout << "\n Entered list as follows:\n";
for( i = 0 ; i<n ; i++)
cout <<" "<< list[i];
i = 0 ;
sort.merge_pass(list, i, n-1);
cout<<"\n Merge sorted list is as follows:\n";
for( i = 0 ; i<n ; i++)
cout <<" "<< list[i];
}
No comments:
Post a Comment