Problem with bubble sorting

metamofia's Avatar
Go4Expert Member
This is my code for bubble sorting:

Code:
 
void CLEO_MedivisionView::OnNoiseremovalMedianfilter()
{
int n=262144;
for(int x=0; x<n; x++)
{
for(int y=0; y<n-1; y++)
{
if(image[y]>image[y+1])
{
int temp = image[y+1];
image[y+1] = image[y];
image[y] = temp;
}
}
}
 
}
When i compile and run this set of codes, my application crashes. Somewhat i have a feeling that the array size of 262144 is the issue. when i just keyed in 262 for the array size (n), i get my image[0] to image[261] sorted in an increasing order. but i cant check for my array size of 262144. is this somewhat of a syntax error or some array overflow issue? what should i do? please help me on this thanks.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No idea; this worked fine for me (btw: see how much clearer the code is when properly indented):
Code:
int image[262144];

void main()
{
	//int image1[10]={8,6,1,9,2,0,5,7,4,3};
	int n=262144;
	int maxy1=0;
	for (int x=0; x<n; x++)
	{
		printf("setting image[%d]=%d\n",x,x%10);
		image[x]=x%10;
	}

	for (int x=0; x<n; x++)
	{
		printf("x iteration %d\n",x);
		for (int y=0; y<n-1; y++)
		{
			if (y+1>maxy1)
				maxy1=y+1;

			if (image[y]>image[y+1])
			{
				int temp = image[y+1];
				image[y+1] = image[y];
				image[y] = temp;
			}
		}
	}
	printf("maxy1=%d\n",maxy1);
	for (int x=0; x<50; x++)
	{
		printf("%d ",image[x]);
	}
	printf("\n");
}
Which version of which compiler are you using?