Problem with bubble sorting

Discussion in 'C' started by metamofia, Nov 2, 2009.

  1. metamofia

    metamofia New Member

    Joined:
    Sep 28, 2009
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    This is my code for bubble sorting:

    Code:
     
    void CLEO_MedivisionView::OnNoiseremovalMedianfilter()
    {
    int n=[B][U]262144[/U][/B];
    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.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice