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

-> This program is to sort the given integers
in ascending order using address calculation sort

-> bins are maintained using linked lists

-> With the hash function used in the program it is
only possible to sort integers that are < 100

-> This program works in microsoft vc++ 6.0 environment.

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

#include<iostream.h>

{
private:
int n;
public:
friend class sorting;
};

{
NEW->n=x;
NEW->next=NULL;

if(a==NULL)
a=NEW;
else          // search for the correct position to insert
{
l=a;
if(x<l->n)
{
NEW->next=l;
a=NEW;
}
else
{
while( l->n<x && l->next!=NULL )
l=l->next;
l->next=NEW;
}
}
return a;
}

{
while(a!=NULL)
{
cout<<a->n<<‘\t’;
a=a->next;
}
cout<<“NULL\n”;
}

class sorting
{
private:
int n;
public:
void input();
void output();
};

void sorting::input()
{
cout<<“*********************************************************\n”;
cout<<“This program sorts the given integers in ascending order\n”
<<”   using address calculation sort algorithm \n”;
cout<<“*********************************************************\n”;

cout<<“Enter how many numbers you are going to enter ::”;
cin>>n;

array=NULL;
cout<<“Now enter your numbers only in the range (0-99) ::\n”;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;

NEW->n=x;
NEW->next=NULL;

if(array==NULL)
array=NEW;
else
l->next=NEW;
l=NEW;
}
}

{
//Hash the numbers in to the five bins

int i;
for(i=1;i<=5;i++)
bin[i]=NULL;

while(array!=NULL)
{
if( array->n >=0 && array->n <=19)
bin[1]=obj.insert(array->n,bin[1] );
else if( array->n >=20 && array->n <=39)
bin[2]=obj.insert(array->n,bin[2] );
else if( array->n >=40 && array->n <=59)
bin[3]=obj.insert(array->n,bin[3] );
else if( array->n >=60 && array->n <=79)
bin[4]=obj.insert(array->n,bin[4] );
else if( array->n >=80 && array->n <=99)
bin[5]=obj.insert(array->n,bin[5] );

array=array->next;
}

cout<<“\nThe contents of the bins are ::\n”;
for(i=1;i<=5;i++)
{
cout<<” ( “<<i<<” ) ::”;
obj.display(bin[i]);
}

//collect from all the bins

array=NULL;
for(i=1;i<=5;i++)
{
while(bin[i]!=NULL)
{

NEW->n=bin[i]->n;
NEW->next=NULL;

if(array==NULL)
array=NEW;
else
l->next=NEW;
l=NEW;

bin[i]=bin[i]->next;
}
}
}

void sorting::output()
{
cout<<“After sorting the elements are ::\n”;
obj.display(array);
}

int main()
{
sorting obj;
obj.input();
obj.output();
return 0;
}
/*****************************************************************

SAMPLE OUTPUT ::

*********************************************************
This program sorts the given integers in ascending order
*********************************************************
Enter how many numbers you are going to enter ::7
Now enter your numbers only in the range (0-99) ::
70
65
60
55
50
45
40

The contents of the bins are ::
( 1 ) ::NULL
( 2 ) ::NULL
( 3 ) ::40     45      50      55      NULL
( 4 ) ::60     65      70      NULL
( 5 ) ::NULL
After sorting the elements are ::
40      45      50      55      60      65      70      NULL
Press any key to continue

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

## 4 thoughts on “Address Calculation Sort”

1. christian rodriguez says:

hello friends pleas help me with the address calculation sort
i need videos or graphics

2. kishan says:

error bahot hai 25+1

1. kishan says: