Search English (United States)  தமிழ் (இந்தியா)
Tuesday, January 06, 2009 ..:: Articles ::.. Register  Login
Location: BlogsArticlesProgramming    
Posted by: ganesh Thursday, September 04, 2008 8:30 AM


This C++ program contains implementation of Mergesort, Heapsort and Quicksort in a single class.I used template for defining set of numbers to sort so the class can be used to sort any type of numeric values. I have also given a main function which utilizes this class for sorting a set of numbers.

Logic of these sorting algorithms can be found in Fundamentals of Data Structures book by Ellis Horowitz and Sartaj Sahni.

#include<iostream.h>
#include
<conio.h>
#define
LIST_SIZE 30
template
<class R>
class
Sort
{
     
private:
     
void Merge(R a[],int f,int s,int t)
     
{
           
R b[LIST_SIZE];
           
int i=f,j=s+1,k=0;
           
while((i<=s)&&(j<=t))
           
{
                 
if(a[i]<a[j])
                 
{
                       
b[k]=a[i];
                       
i++;
     
            }
                 
else
                 
{
                       
b[k]=a[j];
                       
j++;
                 
}
                 
k++;
           
}
           
while(i<=s)
           
{
                 
b[k]=a[i];
                 
i++;
                 
k++;
           
}
           
while(j<=t)
           
{
                 
b[k]=a[j];
                 
j++;
                 
k++;
           
}
           
k--;
           
j=0;

           
for(i=f;i<=t;i++)

           
{
                 
a[i]=b[j];
                 
j++;
           
}
     
}
     
void Heap(R x[], int n, int k)
     
{
           
int j;
           
R t;
           
if((2*k+2<n)&&(x[2*k+2]>x[2*k+1])) j=2; else j=1;
           
if(x[2*k+j]>x[k])
           
{
                 
t=x[2*k+j];

                 
x[2*k+j]=x[k];

                 
x[k]=t;

           
}
     
}
     
public:
     
void MergeSort(R x[],int m,int n)

     
{

           
int q;
           
if(m<n)
           
{
                 
q=(m+n)/2;
                 
MergeSort(x,m,q);
                 
MergeSort(x,q+1,n);
                 
Merge(x,m,q,n);
           
}
     
}
     
void HeapSort(R x[], int n)
     
{
           
R t;
           
if(n>1)
           
{
                 
for(int i=n/2-1;i>=0;i--)
                       
Heap(x,n,i);
                 
t=x[0];
                 
x[0]=x[n-1];
                 
x[n-1]=t;
                 
HeapSort(x,n-1);
           
}
     
}
     
void QuickSort(R x[],int m,int n)

     
{

           
R k,t;

           
int i,j;
           
if(m<n)
           
{
                 
k=x[m];
                 
i=m;
                 
j=n+1;
                 
do
                 
{

                       
do{ i++; }while(x[i]<k);

                       
do{ j--; }while(x[j]>k);
                       
if(i<j)
                       
{
                             
t=x[i];
                             
x[i]=x[j];
                             
x[j]=t;
                       
}
                 
}while(i<j);
                 
x[m]=x[j];
                 
x[j]=k;
     
            QuickSort(x,m,j-1);
                 
QuickSort(x,j+1,n);
           
}
     
}
}

void
main()
{
     
int g[LIST_SIZE],n,i;
     
Sort<int> M;
     
char c;
     
do
     
{
           
clrscr();
           
cout<<"Menu..."<<endl<<"1.Quick Sort"<<endl;
           
cout<<"2.Heap Sort"<<endl<<"3.Merge Sort"<<endl;
           
cout<<"4.Exit"<<endl<<"Enter your choice: ";
           
c=getche();
           
switch(c-48)
           
{
                 
case 1: case 2: case 3:
                 
cout<<endl<<"How many Numbers: ";

                 
cin>>n;

                 
cout<<"Enter Numbers..."<<endl;
                 
for(i=0;i<n;i++) cin>>g[i];
           
}
           
switch(c-48)
           
{
                 
case 1: M.QuickSort(g,0,n-1); break;
                 
case 2: M.HeapSort(g,n); break;
                 
case 3: M.MergeSort(g,0,n-1);
           
}
           
switch(c-48)
           
{
                 
case 1: case 2: case 3:
                 
cout<<"Sorted Numbers..."<<endl;
                 
for(i=0;i<n;i++)
                       
cout<<endl<<g[i];
                 
getch();
           
}
     
}while(c-48!=4);

}

 

Copyright ©2008 Ganesh Kumar
Permalink |  Trackback

Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Copyright 2007 by technicalganesh.com   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2009 by DotNetNuke Corporation