Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Problem with bubble sorting (http://www.go4expert.com/forums/bubble-sorting-t19954/)

metamofia 2Nov2009 12:24

Problem with bubble sorting
 
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.

xpi0t0s 2Nov2009 15:10

Re: Problem with bubble sorting
 
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?


All times are GMT +5.5. The time now is 02:20.