Sign in to follow this  
31337noob

bucketSort help

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[i]%place;
			v2[rowNumber].push_back(v1[i]);
		}

		v1.clear();

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

		place *= 10;

		for(int i = 0; i < this->size; i++)
		{
			cout <<v1[i]<<" ";
		}
		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 this post


Link to post
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[i]%place)/10) % 10;
else
rowNumber = v1[i]%10;
v2[rowNumber].push_back(v1[i]);
}

v1.clear();

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


place *= 10;
ounce = true;

for(int i = 0; i < this->size; i++)
{
cout <<v1[i]<<" ";
}
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 this post


Link to post
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[i]*10)/place) % 10;
else
rowNumber = v1[i]%10;
v2[rowNumber].push_back(v1[i]);
}

v1.clear();

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


place *= 10;
ounce = true;
}

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this