Jump to content
  • Advertisement
Sign in to follow this  
31337noob

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 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*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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!