# bucketSort help

This topic is 4463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

here is my code.....
void BucketSort::sort()
{
int rowNumber = 0;
int place = 10;
while(place <= (this->ran-1))
{
for(int i =0; i < this->size; i++)
{
rowNumber = v1%place;
v2[rowNumber].push_back(v1);
}

v1.clear();

for(int i =0; i < 10; i++)
{
for(int j = 0; j < v2.size(); j++){
v1.push_back(v2[j]);
}
v2.clear();
}

place *= 10;

for(int i = 0; i < this->size; i++)
{
cout <<v1<<" ";
}
cout <<endl;
}
}


now, ran = 101 and v1 is a vector and so is v2. my problem is this, it works the first time trough, but the next time it crashes becasue rowNumber goes over 9. the first time it does this....use 10 23 5 67 68 99 for e.g. rowNumber for 10 = 0 rowNumber for 23 = 3 rowNumber for 5 = 5 rowNumber for 67 = 7 rowNumber for 68 = 8 rowNumber for 99 = 9 it gets the far left spot first. next pass does this. rowNumber for 10 = 1 rowNumber for 23 = 2 rowNumber for 5 = 0 rowNumber for 67 = 6 rowNumber for 68 = 6 rowNumber for 99 = 9 how do i do that....the value can be up to any number so i read the number from right to left by using %, but %10 does not work for the next loop...how do i get it to work right... thanks...

##### Share on other sites
void BucketSort::sort(){	int rowNumber = 0;	int place = 10;	int times = 0;	bool ounce = false;	while(place <= this->ran-1)	{		for(int i =0; i < this->size; i++)		{			if(ounce)				rowNumber = ((v1%place)/10) % 10;				else				rowNumber = v1%10;			v2[rowNumber].push_back(v1);		}		v1.clear();		for(int i =0; i < 10; i++)		{			for(int j = 0; j < v2.size(); j++){				v1.push_back(v2[j]);			}			v2.clear();		}		place *= 10;		ounce = true;		for(int i = 0; i < this->size; i++)		{			cout <<v1<<" ";		}		cout <<endl;	}}

that got values under 100 working, if its over 100, it gets pushed in front and not at the end...why.....
e.g.
100 0 2 3 5 7 18

##### Share on other sites
solved....

void BucketSort::sort(){	int rowNumber = 0;	int place = 10;	int times = 0;	bool ounce = false;	while(place <= (this->ran-1)*10)	{		for(int i =0; i < this->size; i++)		{			if(ounce)				rowNumber = ((v1*10)/place) % 10;			else				rowNumber = v1%10;			v2[rowNumber].push_back(v1);		}		v1.clear();		for(int i =0; i < 10; i++)		{			for(int j = 0; j < v2.size(); j++){				v1.push_back(v2[j]);			}			v2.clear();		}		place *= 10;		ounce = true;	}	cout <<"Sorted:"<<endl;	for(int i = 0; i < this->size; i++)	{			cout <<v1<<" ";	}	cout <<endl;}

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631765
• Total Posts
3002213
×