Skip to main content
GameDev.net gamedev.net
🔒 Locked

bucketSort help

Started by 31337noob May 1, 2006 at 7:43 PM 1 replies 950+ views
Original Post
31337noob
31337noob
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...
31337noob
31337noob
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
31337noob
31337noob
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;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.