#include <iostream>
using namespace std;
#define SIZE 5
class CountSort
{
public:
int *array;
int *array2;
int max_elem;
void initial();
void display();
void Sort_Start();
void Count_Sort(int*,int*,int);
void max();
CountSort()
{
array=new int[SIZE];
array2=new int[SIZE];
}
~CountSort()
{
delete[] array;
delete[] array2;
}
};
void CountSort::initial()
{
for(int i=0;i<SIZE;i++)
{
cout<<"Enter the "<<i<<"th Index = ";
cin>>array[i];
}
}
void CountSort::display()
{
for(int i=1;i<=SIZE;i++)
{
cout<<"The Value of "<<i<<"th Index = ";
cout<<array2[i]<<"\n";
}
}
void CountSort::max()
{
max_elem=array[0];
for(int i=1;i<SIZE;i++)
{
if(array[i]>max_elem)
max_elem=array[i];
}
}
void CountSort::Sort_Start()
{
max();
Count_Sort(array,array2,max_elem);
}
void CountSort::Count_Sort(int *a,int *b,int k)
{
int *c;
c=new int[k+1];
for(int i=0;i<=k;i++)
c[i]=0; //Initial the Array
for(int j=0;j<SIZE;j++)
c[a[j]]=c[a[j]]+1; //Contains number of elements equal to the index
for(int m=1;m<=k;m++)
c[m]=c[m]+c[m-1]; //Cumulative frequence - No. of elements smaller that i
//a[n]=Index to c[i]
//c[a[n]]=position of a[n] in b[]
for(int n=SIZE-1;n>=0;n--)
{
b[c[a[n]]]=a[n];//InPlace Sorting
c[a[n]]=c[a[n]]-1;
}
}
int main()
{
CountSort obj;
obj.initial();
obj.Sort_Start();
obj.display();
return 0;
}
/*
OUTPUT:-
Enter the 0th Index = 10
Enter the 1th Index = 42
Enter the 2th Index = 43
Enter the 3th Index = 545
Enter the 4th Index = 3
The Value of 1th Index = 3
The Value of 2th Index = 10
The Value of 3th Index = 42
The Value of 4th Index = 43
The Value of 5th Index = 545
*/
using namespace std;
#define SIZE 5
class CountSort
{
public:
int *array;
int *array2;
int max_elem;
void initial();
void display();
void Sort_Start();
void Count_Sort(int*,int*,int);
void max();
CountSort()
{
array=new int[SIZE];
array2=new int[SIZE];
}
~CountSort()
{
delete[] array;
delete[] array2;
}
};
void CountSort::initial()
{
for(int i=0;i<SIZE;i++)
{
cout<<"Enter the "<<i<<"th Index = ";
cin>>array[i];
}
}
void CountSort::display()
{
for(int i=1;i<=SIZE;i++)
{
cout<<"The Value of "<<i<<"th Index = ";
cout<<array2[i]<<"\n";
}
}
void CountSort::max()
{
max_elem=array[0];
for(int i=1;i<SIZE;i++)
{
if(array[i]>max_elem)
max_elem=array[i];
}
}
void CountSort::Sort_Start()
{
max();
Count_Sort(array,array2,max_elem);
}
void CountSort::Count_Sort(int *a,int *b,int k)
{
int *c;
c=new int[k+1];
for(int i=0;i<=k;i++)
c[i]=0; //Initial the Array
for(int j=0;j<SIZE;j++)
c[a[j]]=c[a[j]]+1; //Contains number of elements equal to the index
for(int m=1;m<=k;m++)
c[m]=c[m]+c[m-1]; //Cumulative frequence - No. of elements smaller that i
//a[n]=Index to c[i]
//c[a[n]]=position of a[n] in b[]
for(int n=SIZE-1;n>=0;n--)
{
b[c[a[n]]]=a[n];//InPlace Sorting
c[a[n]]=c[a[n]]-1;
}
}
int main()
{
CountSort obj;
obj.initial();
obj.Sort_Start();
obj.display();
return 0;
}
/*
OUTPUT:-
Enter the 0th Index = 10
Enter the 1th Index = 42
Enter the 2th Index = 43
Enter the 3th Index = 545
Enter the 4th Index = 3
The Value of 1th Index = 3
The Value of 2th Index = 10
The Value of 3th Index = 42
The Value of 4th Index = 43
The Value of 5th Index = 545
*/
0 comments:
Post a Comment