Issues With Sorting Program

This is a discussion on Issues With Sorting Program within the C Programming forums, part of the General Programming Boards category; I'm attempting to write a function to sort a series of values into asscending order: Code: void sort(float numbers[]) /* ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    6

    Issues With Sorting Program

    I'm attempting to write a function to sort a series of values into asscending order:

    Code:
    void sort(float numbers[])       /* function to sort array */
    {
            i = 0;
            while( i < (size) ) 
                    {
                    j = i + 1;
                    while( j <= size ) 
                            {
                            if( A[i] > A[j] ) 
                                    {
                                     temp = A[i];
                                     A[i] = A[j];
                                     A[j] = temp;
                                     }
                             j++;
                            } 
                    i++;
                    }
                    for (i = 0; i < size; i++)        
        		    		printf("\nThe sorted x-values are: (%f)", A[i]); /* displays (x,y) cooridinates */
                            printf("\n");
    }
    Now, it compiles and runs fine but it seems to set the largest value in the array to zero and then re-order the values as it should. Any idea why it's doing that?

    Many thanks,

    Chris

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    First of all it's not a good idea to have global variables in your code when you dont need them to be global.Next your "j" counter is running up to size whereas it should have been size-1.

    Here's an example on how I would go about writing that function:-

    Code:
    void sort(float array[],int size)
    {
    	int i,j;
    	float temp=0;
    	
    	for(i=0;i<size;i++)
    	{
    		for(j=0;j<size;j++)
    		{
    			if(array[i] > array[j])
    			{
    				temp=array[i];
    				array[i]=array[j];
    				array[j]=temp;
    			}
    		}
    	}
    }
    Make sure you fully understand how the above code works and why yours wont, try making a few hand-runs through the code that will make you see things clearer. Also learn how to use a debugger, the errors you are making can be easily caught if you know how to step through your code and watch variables.
    Thanks,
    Sahil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Continued issues with Winows Errors when running program
    By hpteenagewizkid in forum C Programming
    Replies: 6
    Last Post: 11-14-2006, 02:51 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Sorting Program... :(
    By thynksheraze in forum C++ Programming
    Replies: 6
    Last Post: 11-05-2003, 06:01 AM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 10:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21